In PHP, there are two ways to access a class's methods/variables:
1. Create the object $object = new Class (), and then use the "-and" call: $object->attribute/function, provided the variable/method is accessible.
2. Call the class method/variable directly: class::attribute/function, either static/non-static. But there are prerequisites:
A. If it is a variable, it needs to be accessible.
B. In the case of a method, in addition to the method's accessibility, it is necessary to satisfy:
B1) If it is a static method, there is no special condition;
B2) If the method is non-static, it is not necessary to use $this, that is, the non-static variable/method is not called, of course, there is no problem in calling the static variable/method.
Then we'll look at the use of $object-> and use class:: ... What is the difference:
1. Use $object->.., you need to execute the constructor to create the object;
2. Use class:: ... Call static method/variable, do not need to execute constructor to create object;
3. Use class:: ... Call a non-static method/variable and do not need to execute the constructor to create the object.
Then the strange place came out, since 2 and 3 are the same, what is the meaning of static method/variable existence?
The difference is still obvious, as follows:
1. Static variables
A static member retains only one variable value, and this variable value is valid for all instances, that is, all instances share this member.
2. Static methods
Static methods can be used directly with the class:: ... Instead of static methods that require certain restrictions to use class::. Method calls, as described earlier
http://www.bkjia.com/PHPjc/324956.html www.bkjia.com true http://www.bkjia.com/PHPjc/324956.html techarticle in PHP, there are two ways to access a class's methods/variables: 1. Create Object $object = new Class (), and then use "-" Call: $object-attribute/function If the variable/method can be accessed ...