Analysis of the memory location of static statics properties and methods in object-oriented PHP _php tips

Source: Internet
Author: User

In this paper, we analyze the memory location of statically static properties and methods in object-oriented PHP. Share to everyone for your reference. Specifically as follows:

The memory location of static statically properties--> classes, not objects. Here's a test to prove

<?php
Header ("Content-type:text/html;charset=utf-8");
Class human{
 static public $name = "little sister";
 public $height;
 Public function tell () {
 }
}
echo Human: $name;
Without relying on the object, you can access it directly. Because the memory location of the static property is in the class, not the object.
$p 1 = new Human ();
$p 2 = new Human ();
Print_r ($p 1);
echo $p 1:: $name = "Lady";
Change the value of the static property on the $p1 object, and the $p2 object will change accordingly.
echo $p 2:: $name;
? >

The results of the output can be seen:

1, Echo Human: $name: The class declaration is complete, there is a static property, does not depend on the object. Therefore, there is only one static property (meaning in memory, the location is not in the object, if in the object, that instantiate an object, there is the corresponding static position, such as the Height property);

2, Print_r ($p 1): Print results only the Height property, and no name;

3. When the value of a static property changes, the value of the property for all objects will be affected.

For methods, whether static or normal, they exist in the class memory space. Proof is also very simple, new an object, Print_r (object) can be.

I hope this article will help you with your PHP program design.

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.