开发者

Can't pass a variable by reference

开发者 https://www.devze.com 2023-03-13 14:31 出处:网络
I am really confused with this one. Here\'s my code class i开发者_StackOverflow社区nner { var $val = 0;

I am really confused with this one. Here's my code

class i开发者_StackOverflow社区nner
{
    var $val = 0;   
}

class outer
{
    var $obj=null;

    function outer()
    {
        $this->obj = new inner;
    } 

    function get_obj()
    {
        return $this->obj;
    }
}


$app = new outer;

$obj = &$app->get_obj(); //get object by reference (& is not necessary in PHP5)
$obj->val = 1; //change something
echo $app->obj->val; //check whether it affected the original object source
//here it should display 1

When I am testing this on my local server, which is PHP 5.2.10-2ubuntu6.4, everything works fine and it displays 1. When I am testing this on my customer's PHP Version 5.2.9 server, it displays 0. Is there some known PHP bug or may be some php.ini setting that could affect this behavior?


PHP5 and above will pass all objects as a reference. Everything else gets passed by value.

But this may not be the case if a certain directive is enabled. Check if zend.ze1_compatibility_mode in your php.ini is active. Taken from the PHP docs:

[...] It affects the cloning, casting (objects with no properties cast to FALSE or 0), and comparing of objects. In this mode, objects are passed by value instead of reference by default.

Hope this solves the problem...


If not works, try replace this

function get_obj()

By this

function &get_obj()

Usefull links:
http://php.net/manual/en/language.references.pass.php http://php.net/manual/en/language.oop5.references.php


Check the client's php.ini file, there's gotta be something fishy. I'm searching on google, I can't find any bugs that people have had with call by reference on 5.2.9.

And, try azat's suggestion.

0

精彩评论

暂无评论...
验证码 换一张
取 消