PHP Global and static variable reference problem?

Source: Internet
Author: User
Which great God can explain this example in the official PHP manual from a memory point of view, I am a beginner PHPerDo not understand, please the elders of the liberal enlighten, the younger generation greatly thanks!

It is best to be able to run the following code 内存 under the scenario description.

Similar behavior applies to static statements as well. References are not stored statically:

function &get_instance_ref() {    static $obj;    echo 'Static object: ';    var_dump($obj);    if (!isset($obj)) {        // 将一个引用赋值给静态变量        $obj = &new stdclass;    }    $obj->property++;    return $obj;}function &get_instance_noref() {    static $obj;    echo 'Static object: ';    var_dump($obj);    if (!isset($obj)) {        // 将一个对象赋值给静态变量        $obj = new stdclass;    }    $obj->property++;    return $obj;}$obj1 = get_instance_ref();$still_obj1 = get_instance_ref();echo "\n";$obj2 = get_instance_noref();$still_obj2 = get_instance_noref();

The above routines will output:

Static object: NULLStatic object: NULLStatic object: NULLStatic object: object(stdClass)(1) {["property"]=>int(1)}

The example above demonstrates that when a reference is assigned to a static variable, the value of the second call to the &get_instance_ref () function is not remembered.

The source of this example: http://php.net/manual/en/language.variables.scope.php

Reply content:

Which big God can from the memory angle to explain the PHP Official Handbook of this example, I am a beginner PHPer do not quite understand, please the elders of the liberal enlighten, the juniors greatly thanks!

It is best to be able to run the following code 内存 under the scenario description.

Similar behavior applies to static statements as well. References are not stored statically:

function &get_instance_ref() {    static $obj;    echo 'Static object: ';    var_dump($obj);    if (!isset($obj)) {        // 将一个引用赋值给静态变量        $obj = &new stdclass;    }    $obj->property++;    return $obj;}function &get_instance_noref() {    static $obj;    echo 'Static object: ';    var_dump($obj);    if (!isset($obj)) {        // 将一个对象赋值给静态变量        $obj = new stdclass;    }    $obj->property++;    return $obj;}$obj1 = get_instance_ref();$still_obj1 = get_instance_ref();echo "\n";$obj2 = get_instance_noref();$still_obj2 = get_instance_noref();

The above routines will output:

Static object: NULLStatic object: NULLStatic object: NULLStatic object: object(stdClass)(1) {["property"]=>int(1)}

The example above demonstrates that when a reference is assigned to a static variable, the value of the second call to the &get_instance_ref () function is not remembered.

The source of this example: http://php.net/manual/en/language.variables.scope.php

Invoking a non-static method in a static manner results in an e_strict level error.

Just like all other PHP static variables, static properties can only be initialized to literals or constants, and expressions cannot be used. You can initialize a static property to an integer or an array, but you cannot initialize it to another variable or function return value, or point to an object.
Source: http://php.net/manual/zh/language.oop5.static.php

So global and static do not use reference assignment, which is determined by the PHP interpreter.

  • 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.