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?
Static statics
Declaring a class member or method as static can be accessed directly without instantiating a class, and cannot access static members through an object except static methods. Static members belong to a class and do not belong to any object instance, but object instances of the class can be shared.
Example:
<?PHPClassperson{//defining static member properties Public Static $country= "China"; //defining static Member Methods Public Static functionMycountry () {//internal access static member properties Echo"I am." Self::$country." People <br/> "; }}classStudentextendsPerson {functionStudy () {Echo"I am". Parent::$country." People <br/> "; }}//Output member property valueEchoPerson::$country." <br/> ";//Output: China$p 1=NewPerson ();//Echo $p 1->country; Error notation//Access static Member methodPerson::mycountry ();//output: I am Chinese//static methods can also be accessed through objects:$p 1-mycountry ();//output member property values in subclassesEchoStudent::$country." <br/> ";//Output: China$t 1=NewStudent ();$t 1->study ();//output: I am a Chinese?>
To run the example, output:
China
I am a Chinese
I am a Chinese
China
I am a Chinese
Summary
To access static member properties or methods inside a class, use self:: (note not $slef), such as:
$country slef:: Mycountry ()
To access the parent class static member property or method in the subclass, use Parent:: (note not $parent), such as:
$country parent:: Mycountry ()
External access static member properties and methods are class name/subclass name::, such as:
Person::$countryPerson::mycountry () Student::$country
However, static methods can also be accessed by ordinary objects
The difference between static statically class and static variable usage in PHP