1, static properties, static methods
In object-oriented programming, we can not only access methods and properties through objects, but also access them through classes. Such methods and properties are static and must be declared with the static keyword.
[PHP]View Plaincopyprint?
- Class Staticexample {
- Staticpublic $num = 0;
- Staticpublic function Saynum () {
- Echoself::num;
- }
- }
A static method is a function that takes a class as a scope. Static methods cannot access normal properties in this class, because those properties belong to an object, but static properties can be accessed. If you modify a static property, all instances of the class will have access to the new value.
Because static elements are accessed through classes instead of instances, accessing static elements does not require referencing the variables of the object, but instead uses:: (two colons) to concatenate the class name and property or class name and method.
[PHP]View Plaincopyprint?
- Echostaticexample::$num;
- Staticexample::saynum ();
To access a static method or property from the current class (not a subclass), you can use the Self keyword. Self points to the current class as if the pseudo-variable $this points to the current object. Therefore, you can use the class name to access the property $num outside of the Staticexample class:
[PHP]View Plaincopyprint?
- Staticexample::$num
Inside the Staticexample class, you can use the Self keyword
Use reason:
1. They are available anywhere in the code (assuming you can access the class), that is, you do not need to pass an instance of the class between objects, and you do not need to store the instance in a global variable to access methods in the class
2. Each instance of a class can access the static properties defined in the class, so you can use a static property to set the value that can be used by all objects of the class
3. You can access a static property or method without instantiating the object, so that we don't have to instantiate the object in order to get a simple function
2. Constant properties
Some properties cannot be changed.
In PHP 5 You can define a constant property in a class, and as with a global variable, a class constant cannot be changed once it is set. Constant attributes are declared with the Const keyword. Constants do not start with $ as regular properties do. By convention, you can only name constants in uppercase letters, as follows:
[PHP]View Plaincopyprint?
- Class Shopproduct {
- constavailable = 0;
- //...
- }
A constant property contains only the values of the base data type. You cannot assign an object to a constant. Like static properties, constant properties can only be accessed through the class and not through an instance of the class. You do not need to use $ as a leader when referencing constants, as follows:
[PHP]View Plaincopyprint?
- echoshopproduct::available;
Assigning a value to a constant that has already been declared causes a parsing error.
You should use constants when you need to have access to a property in all instances of the class, and if the property value does not need to be changed.
Constant properties can only be accessed through a class and not through an instance of the class