Call sharing for static and static methods in PHP

Source: Internet
Author: User
In this paper, we analyze the static and static methods in the object-oriented PHP call. About their invocation (can be called, how to call), need to figure out where they are stored in memory, so it is very easy to understand. Static properties, methods (both static and non-static) in memory, only one location (not a static property, how many objects are instantiated, and how many properties).

Instance:

<?phpheader ("Content-type:text/html;charset=utf-8"), class human{staticpublic $name = "little Sister"; public$height = 180; Staticpublic Functiontell () {echoself:: $name;//static method call static property, use self keyword//echo $this->height;//error. A static method cannot call a non-static property//Because the $this represents the instantiated object, and here is the class, not knowing which object the $this represents} publicfunction say () {echoself:: $name. "I have spoken"; The normal method calls the static property, also uses the Self keyword echo$this->height; }} $p 1 = new Human (); $p 1->say (); $p 1->tell ();//object can access static method echo $p 1:: $name;//object access static property. You cannot access $p1->name//because the memory location of the static property is not in the object Human::say ();//Wrong. Say () $this error when no $this is available//But php5.4 above will prompt?>

(1), static properties do not need to be instantiated to invoke. Because the static property is stored in a class, the calling method is "class name:: Attribute name"; Conclusion:

(2), static methods do not need to be instantiated to invoke. Ditto
(3), static methods cannot invoke non-static properties. Because the non-static attribute needs to be instantiated, it is stored in the object;
(4), static methods can call non-static methods, using the Self keyword. In PHP, a method is self: after that, it automatically transforms into a static method;

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.