Differences between static php methods and common methods

Source: Internet
Author: User
The following small series will give you a detailed description of the differences between php static methods and common methods. I think this is quite good. now I will share it with you and give you a reference. Let's take a look at the following:

 ';} 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! The method can execute Human: eat ();/* The following method easyeat is a non-static method, which is called by the object. However, it can also be executed to call this method using classes, in a Strict state, this method is executed and an error is reported at the same time. Strict Standards: Non-static method Human: easyeat () shoshould not be called statically in D: \ application \ PHPnow-1.5.6 \ htdocs \ yan18 \ types \ staticfun. php on line 32 */Human: easyeat ();/*. it can be understood from the logic. if the class name is used to call a non-static (common) method statically, 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, in fact, non-static methods cannot be called statically by class names, but currently, the object-oriented detection in php is not strict enough. as long as there is no $ this keyword in the static method, it will be converted into a static method for processing! */$ Li = new Human (); $ li-> eat ();/* Summary: Class access-> static method (class method) -> Class access-> Common method (object method)-> no (although the $ this keyword is not used in the method, yes! But this method is not supported.) access to object static method (class method)-> access to object normal method (object method)-> Yes */?>

The above is all about the differences between static php methods and common methods. I hope you can support PHP ~

For more details about the differences between php static methods and common methods, please refer to the PHP Chinese website!

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.