tags:

views:

48

answers:

3

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.

Thanks

+4  A: 

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

Carl Norum
A: 

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

a1ex07
A: 

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