开发者

What is the difference between const and static vars?

If I have a class and want to have some static vars, which would be the correct way to declare them?

For example

class Foo{

    public static $var1

    public static $var2

    public static $var3

    ......

}

OR

class Foo{

    const $var1

    const $var2

    const $var3

    ......

}

both ways let me use Foo::var1. But I dont know w开发者_Python百科ell which is the correct way and/or if there is any adventage using one way or the other.


const variables can't be modified. The static ones can. Use the right one for what you're trying to do.


class Foo{const $var1....} is a syntax error (don't need $ after const)


In conventional coding standards we name consts like Foo::CONST1 in all caps without any $ in the name. So you don't need to be confused between consts and variables.

As others have noted, you define the value of a class constant once, and you can't change that value during the remainder of a given PHP request.

Whereas you can change the value of a static class variable as many times as you need to.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜