Static modified class attributes

Source: Internet
Author: User

I read the book and said: static members are always unique and can be shared among multiple objects.

Therefore, if I. person is instantiated in PHP. class. PHP class and assign a value to static $ name. if this class is instantiated again in PHP, can B not read the new value of the name attribute?

The Code is as follows:

Person. Class. php

<span style="font-size:14px;"><?php    class person{        public static $name;        function __construct($name='vv'){            //self::$name = $name;        }        function getName(){            return self::$name;        }    }?></span>

 

A. php

<span style="font-size:14px;"><?php     include 'Person.class.php';    $ren = new Person();    Person::$name = 'aa';    echo $ren -> getName(); //aa    $ren2 = new Person();    echo $ren2 -> getName(); //aa?></span>

 

B. php

<span style="font-size:14px;"><?php     include 'Person.class.php';    $ren3 = new Person();    echo $ren3 -> getName(); //null?></span>


It turns out that static members can only be shared among multiple objects in the same script. B. PHP cannot read. the property value set by PHP was taken for granted. Because PHP can enable multiple processes to execute the script at the same time, and the memory is automatically released after the script is executed, B. PHP cannot read. PHP properties!

 

Complete static usage instructions: see PHP details page 284th.

 

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.