0

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 well which is the correct way and/or if there is any adventage using one way or the other.

hakre
  • 193,403
  • 52
  • 435
  • 836
Ivano
  • 72
  • 6
  • Possible duplicate of http://stackoverflow.com/questions/1685922/php5-const-vs-static and http://stackoverflow.com/questions/4163975/php-oop-constant-vs-static-variables. – johankj Dec 24 '12 at 22:12

3 Answers3

4

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

Carl Norum
  • 219,201
  • 40
  • 422
  • 469
0

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

a1ex07
  • 36,826
  • 12
  • 90
  • 103
0

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.

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828