Class constants
Class constants are just like regular constants, except for the fact that they are declared on a class and therefore also accessed through this specific class. Just like with static members, you use the double-colon operator to access a class constant. Here is a basic example:
<?php class User { const DefaultUsername = "John Doe"; const MinimumPasswordLength = 6; } echo "The default username is " . User::DefaultUsername; echo "The minimum password length is " . User::MinimumPasswordLength; ?>As you can see, it's much like declaring variables, except there is no access modifier - a constant is always publically available. As required, we immediately assign a value to the constants, which will then stay the same all through execution of the script. To use the constant, we write the name of the class, followed by the double-colon operator and then the name of the constant. That's really all there is to it.
© php5-tutorial.com 2007-2012