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:

<?php

class MyClass{

public final static $finalVariable = "something";
}

MyClass::$finalVariable


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

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:

<?php

class MyClass{
    const finalVariable = "something";
}

MyClass::finalVariable;
?>

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