Memory location analysis of static attributes and methods in php object-oriented language _ PHP

Source: Internet
Author: User
This article mainly introduces the memory location of static attributes and methods in php object orientation, and analyzes the principles and usage skills of static attributes through memory location instances, for more information, see the examples in this article to analyze the static attributes of php object and the memory location of the method. Share it with you for your reference. The details are as follows:

Static attribute memory location --> class, not an object. The following is a test to prove it.

<? Phpheader ("content-type: text/html; charset = utf-8"); class Human {static public $ name = "Xiaomei"; public $ height; public function tell () {}} echo Human: $ name; // directly accessible without relying on objects. Because the memory location of the static attribute is in the class, not the object. $ P1 = new Human (); $ p2 = new Human (); print_r ($ p1); echo $ p1: $ name = "Mrs "; // change the value of the static attribute on the $ p1 object, and the $ p2 object will also change accordingly. Echo $ p2: $ name;?>

The output result shows that:

1. echo Human: $ name: After the class declaration is complete, the static attribute exists and does not depend on the object. Therefore, there is only one static attribute (in the memory, the storage location is not in the object; if it is in the object, it instantiates an object, the corresponding static location, such as the height attribute );

2. print_r ($ p1): the printed results only have the height attribute, but not the name;

3. when the value of a static attribute changes, the attribute value of all objects will be affected.

For methods, both static and common methods exist in the class memory space. The proof is also very simple. a new object, print_r (object), is enough.

I hope this article will help you with php programming.

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.