The difference between PHP static methods and common methods

Source: Internet
Author: User
As shown below:

<?phpheader (' content-type:text/html;charset=utf-8 ');/* Normal method, storage class, only a static method, is also stored in the class, only one difference is: The ordinary method requires the object to call, need to bind $  This is, the normal method, must have the object, and then let the object to invoke the static method, which does not belong to which object, so do not need to bind $this that is, do not need the object can also call */class human{static public $head = 1;  Public Function Easyeat () {echo ' common method meal <br/> ';  } static Public Function eat () {echo ' static method eats <br/> ';  } public Function Intro () {echo $this->name; }}error_reporting (e_all| E_STRICT);//There are no objects at this time! The method can execute Human::eat ();/* The following method Easyeat is a non-static method that is called by the object, but it can also be executed using the class to invoke this method, while in strict state this method executes with an error, STRICT standards: Non-static method Human::easyeat () should not being called statically in D:\application\PHPnow-1.5.6\htdocs\yan18\types\ staticfun.php on line */human::easyeat ();/* Connect, from the logic to understand, if the class name static call non-static (normal) method such as: Intro () So, this $this refers to which object?? This will result in an error 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 Lin e 23*/human::intro (); /* As above analysis, in fact, non-static method can not be called statically by the class name, but at present, the object-oriented detection in PHP is not strict enough, as long as the static method does not have $this keyword, it will be converted to static method to deal with! */$Li=new Human (); $li->eat (); /* Summary: Class Access-static method (method of Class)--class access---general method (method of object)------------------------No, although the method does not use $this keyword! But this is not supported) object access static method (method of Class)-can object Access "common method (object method)---" */?>

The above is a small series for everyone to discuss the PHP static method and the common method of the difference between the whole content, I hope that we support topic.alibabacloud.com~

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.