David David -4 years ago 110
PHP Question

Do public static final class variables exist

In Jave you can define a

public static final
variable in a class. Is there an equivalent to this in PHP?

I'd like to do the following:


class MyClass{

public final static $finalVariable = "something";


and not ever have to worry about
changing and not having a new instance for every instantiation of

Answer Source

From this page in the PHP manual:

Note: Properties cannot be declared final, only classes and methods may be declared as final.

However, you can use class constants as described here.

Your example would look something like this:


class MyClass{
    const finalVariable = "something";


Except of course that finalVariable isn't really an appropriate name because it's not variable =).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download