Differences between static php methods and common methods,
Header ('content-type: text/html; charset = utf-8 ');
/*
The common method stores only one copy of the class.
Static methods are also stored in the class, with only one copy
The difference is that a common method requires an object to call and needs to be bound to $ this
That is, a common method must have an object, and then let the object call
The static method does not belong to any object, so you do not need to bind $ this
That is, you can call
*/
Class Human {
Static public $ head = 1;
Public function easyeat (){
Echo 'normal way to eat
';
}
Static public function eat (){
Echo 'static method for dinner
';
}
Public function intro (){
Echo $ this-> name;
}
}
Error_reporting (E_ALL | E_STRICT );
// No object at this time! Method can be executed
Human: eat ();
/*
The following method easyeat is a non-static method called by an object. However, it can also be executed by calling this method using a class. in a strict state, this method is executed and an error is reported,
Strict Standards: Non-static method Human: easyeat () shocould not be called statically in D: \ application \ PHPnow-1.5.6 \ htdocs \ yan18 \ types \ staticfun. php on line 32
*/
Human: easyeat ();
/*
To understand the logic, if the class name is used to call a non-static (common) method
For example, intro ()
Which object does this $ this mean ??
Therefore, an error is reported because the object cannot be found!
Fatal error: Using $ this when not in object context in D: \ application \ PHPnow-1.5.6 \ htdocs \ yan18 \ types \ staticfun. php on line 23
*/
Human: intro ();
/*
In the above analysis, non-static methods cannot be called statically by class names. However, currently, object-oriented detection in php is not strict enough, as long as the $ this keyword is not found in static methods, it will be converted to a static method for processing!
*/
$ Li = new Human ();
$ Li-> eat ();
/*
Summary:
Class access-> static method (class method)-> Yes
Class access-> Common method (object method)-> no (although the $ this keyword is not used in the method, yes! However, this method is not supported)
Object Access static method (class method)-> Yes
Object Access normal method (object method)-> Yes
*/
?>