Details the difference between PHP static method and common method _php instance

Source: Internet
Author: User

The

looks like this:

<?php header (' Content-type:text/html;charset=utf-8 '); /* Common method, storage class, only a static method, but also stored in the class, only one difference is: The common method requires the object to invoke, the need to bind $this that is, the normal method, must have an object, and then let the object to invoke the static method, does not belong to which object, so do not need to bind $this 
  , you do not need an object or call the/class human{static public $head = 1; 
  Public Function Easyeat () {echo ' common method of eating <br/> '; 
  The static public function eat () {echo ' static method eats <br/> '; 
  The Public Function intro () {echo $this->name; }} error_reporting (e_all| 
E_STRICT); No object at this time! 
Method can perform human::eat (); /* The following method Easyeat is a non-static method that is invoked by the object, but it can also be executed by calling this method with a class, and in a strict state, this method executes, with an error, Strict standards:non-static methods Human:: 
Easyeat () should not is called statically in D:\application\PHPnow-1.5.6\htdocs\yan18\types\staticfun.php to line 32 * * 
Human::easyeat (); 
/* Connect, from the logic to understand, if the static invocation of the class name Non-static (normal) method such as: Intro () So, this $this refers to which object?? 
Therefore the error, because the object can not be found! Fatal error:using $this When isn't in the object context into D:\application\PHPnow-1.5.6\htdocs\yan18\types\staticfun.php on Lin

E * * Human::intro (); /* As above analysis, itsReal, non-static method, can not be called by the class name static, but at present, the object-oriented detection in PHP is not strict enough, as long as static methods do not have the $this keyword, will be converted to static methods to deal with! 
* * $li =new Human ();

$li->eat (); /* Summary: Class access-> static method (method of Class)-> can access-> Common method (method of object)-> not (although the method does not use the $this keyword, you can!)
 But does not support this type of writing) object "Access" static method (Method of Class)-> can object "Access" common Method (object method)-> can be * * *?>

The above is a small series for everyone to talk about the static method of PHP and common methods of the difference in all the content, I hope that we support cloud-Habitat Community ~

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.