<?php
Constants in PHP
Define (' MYNUM ', 2000);
echo MYNUM. ' <br> ';
if (!defined (' MYNUM ')) {
Define (' MYNUM ', 4000);
}
Echo MYNUM;
In-class declaration and use
Class Testconst {
Const COUNTRY = ' China '; Const cannot be public, static does not need $, variable name is uppercase
static $static = ' static ';
Public Function Getcountry ()
{
Echo ' accesses the const attribute inside the class: '. Self::country. ' <br> '; In-class calls must be in the form of self (self+ range resolution operator + static variable name)
Echo ' accesses the static property inside the class: '. Self:: $static. ' <br> '; In-class calls must be in the form of self (self+ range resolution operator + static variable name)
Echo ' accesses the static function method inside the class: '. Self::test2 (). ' <br> '; Call static function method inside class self::+ static resource Name = = = This is the standard format
}
public static function Test2 ()
{
Echo '-I am a static method of function-';
}
}
$test = new Testconst ();
Var_dump ($test);
echo $test->getcountry (). ' <br> ';
Echo ' accesses the const attribute on the outside of the class: '. Testconst::country. ' <br> '; Const can be called directly using (the name of the class + range resolution operator + static variable name)
Echo ' External access to static properties of class: '. Testconst:: $static. ' <br> '; Static can be called directly using (the name of the class + range resolution operator + static variable name)
Echo ' accesses the static function method inside the class: '. Testconst::test2 (). ' <br> '; Call static function method self::+ static resource name in class
?>
<!--in-script declaration and use-
<?php
Const COUNTRY = ' Japan ';
Echo Country. ' <br> ';
?>
Constant Const property, static property, static function method in PHP