This is the sixth day of my participation in Gwen Challenge
As the book continues, after member variables and methods, the other component of another class is a constant.
Constants can be defined using the const keyword. You do not need to use the $sign when defining and using constants.
The value of a constant must be a fixed value, not a variable, class attribute, result of a mathematical operation, or function call. Values that remain constant throughout a class can be defined as constants.
Constants can be accessed either by objects or directly by :: constant names. $this, self, parent, and static; If it is outside the class, it can only be the class name, object, or member method.
Let’s look at constant access as an example:
class MyClass { const constant = 'constant value'; // Define constants and assign values; function showConstant() { echo self::constant . "\n"; }} echo MyClass::constant. "\n"; $className = "MyClass"; echo $classname::constant . "\n"; $class = new MyClass(); $class->showConstant(); Echo $class::constant."\n"; // Access through objectsCopy the code
Constants are inherited, or overridden, by subclasses by defining constants of the same name:
class AP { const X=1; const Y=self::X; // Constant can be defined by expression, but the value of the expression must be constant; function output() { return $this::X; }} class BP extends AP {const X=2; // Subclass overrides the parent constant; } var_dump(BP::Y); //int(1) outputs the constant value inherited from the parent class; $output=new BP; var_dump($output->output()); //int(2) outputs the constant value of the subclass;Copy the code
Likewise, constants have visibility public, private, and protected:
- Public can be accessed globally;
- Private can only be accessed within this class;
- Protected can be accessed by itself as well as its subclasses and superclasses;
Let’s look at the effect of visibility on constants:
class MyC { public const MY_PUBLIC = 'public'; // public const MY_PROTECTED = 'protected'; Private const MY_PRIVATE = 'private'; Public function foo() {echo self::MY_PUBLIC; echo self::MY_PROTECTED; echo self::MY_PRIVATE; } } $myclass = new MyC(); echo MyC::MY_PUBLIC; //public accesses the public constant MyC::MY_PROTECTED; MyC::MY_PRIVATE; MyC::MY_PRIVATE; $myclass->foo(); //Public Protected Private, call the member method to print all visible constants of the class.Copy the code
Let’s look at the effect on subclass inheritance:
{function foo2() {echo self::MY_PUBLIC; echo self::MY_PUBLIC; echo self::MY_PROTECTED; echo self::MY_PRIVATE; $myclass2 = new MyC2;} $myclass2 = new MyC2; $myclass2->foo2(); //Public Protected, parent private constants cannot be output;Copy the code
To sum up:
- Constant assignments must be constant.
- Constant access requires :: double quotes.
- Subclasses inherit public and protected constants from their parent class, not private constants.
- Subclasses can overwrite constants of their parent class.
- Visibility limits apply to constants as well.
Thank you for reading, if there are inaccurate and wrong place welcome comment correction, I will timely correction, thank you!
Resources: www.php.net PHP official documentation