Differences between static php methods and common methods

Source: Internet
Author: User
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

*/
?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.