【问题标题】:PHP 5: return by ref method produces unexpected resultsPHP 5:通过 ref 方法返回会产生意想不到的结果
【发布时间】:2012-07-03 14:38:55
【问题描述】:

我创建了一个简单的类来管理一个看起来很奇怪的树数据结构(代码如下)。当很明显这不是我的错误时,我创建了一个测试用例,它产生了同样令人费解的行为。这在 5.3 和 5.4 中是一样的。

这是我的测试用例:

<?php
class testcaseA {
    public function __construct($one=0, $two=1, $three=2) {
        $this->obj = new testcaseB($one++, $three, $two);
    }
    public function &get($what){
        return $this->obj->get($what);
    } 
}
class testcaseB {
    public function __construct($one, &$two, &$three) {
        $this->one=$one;
        $this->two=$two;
        $this->three=$three;
        echo "\$one={$one}";
    }
    public function &get($what){
        echo "<p>You asked for $what. $what ain't no country I ever heard of.<br />";
        echo "Check: [{$this->one}], {$this->two}, {$this->three}. Is this thing on?</p>";
        $this->obj[$what] = new testcaseB($this->one++,$this->two,$this->three);
        return $this->obj[$what];
    } 
}
ini_set('display_errors',1); 
error_reporting(E_ALL);
$bob = new testcaseA();
$bob->get("What")->get("Spam")->get("America");
$bob->get("What")->get("EU")->get("France");
echo "<pre>";
print_r($bob);

现在我期望的输出是看到 $one 的值递增 1,2,3,1,2,3 并产生一个树形。

这是我实际得到的输出:

$one=0

You asked for What. What ain't no country I ever heard of.
Check: [0], 2, 1. Is this thing on?
$one=0

You asked for Spam. Spam ain't no country I ever heard of.
Check: [0], 2, 1. Is this thing on?
$one=0

You asked for America. America ain't no country I ever heard of.
Check: [0], 2, 1. Is this thing on?
$one=0

You asked for What. What ain't no country I ever heard of.
Check: [1], 2, 1. Is this thing on?
$one=1

You asked for EU. EU ain't no country I ever heard of.
Check: [1], 2, 1. Is this thing on?
$one=1

You asked for France. France ain't no country I ever heard of.
Check: [1], 2, 1. Is this thing on?
$one=1

testcaseA Object
(
    [obj] => testcaseB Object
        (
            [one] => 2
            [two] => 2
            [three] => 1
            [obj] => Array
                (
                    [What] => testcaseB Object
                        (
                            [one] => 2
                            [two] => 2
                            [three] => 1
                            [obj] => Array
                                (
                                    [EU] => testcaseB Object
                                        (
                                            [one] => 2
                                            [two] => 2
                                            [three] => 1
                                            [obj] => Array
                                                (
                                                    [France] => testcaseB Object
                                                        (
                                                            [one] => 1
                                                            [two] => 2
                                                            [three] => 1
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

起初这让我很困惑,但我想知道链接使用是否以我没有预料到的方式设置值。

所以我尝试了这个我在之前的代码之后添加的:

...same classes and initial code as before...
$a=$bob->get("What");
$b=$a->get("Spam");
$c=$b->get("America");
//$bob->get("What")
$d=$a->get("EU");
$e=$d->get("France");  
print_r($bob);

这产生了一组不同但仍无法预测的结果。

You asked for What. What ain't no country I ever heard of.
Check: [2], 2, 1. Is this thing on?
$one=2

You asked for Spam. Spam ain't no country I ever heard of.
Check: [2], 2, 1. Is this thing on?
$one=2

You asked for America. America ain't no country I ever heard of.
Check: [2], 2, 1. Is this thing on?
$one=2

You asked for EU. EU ain't no country I ever heard of.
Check: [3], 2, 1. Is this thing on?
$one=3

You asked for France. France ain't no country I ever heard of.
Check: [3], 2, 1. Is this thing on?
$one=3testcaseA Object
(
    [obj] => testcaseB Object
        (
            [one] => 3
            [two] => 2
            [three] => 1
            [obj] => Array
                (
                    [What] => testcaseB Object
                        (
                            [one] => 4
                            [two] => 2
                            [three] => 1
                            [obj] => Array
                                (
                                    [Spam] => testcaseB Object
                                        (
                                            [one] => 3
                                            [two] => 2
                                            [three] => 1
                                            [obj] => Array
                                                (
                                                    [America] => testcaseB Object
                                                        (
                                                            [one] => 2
                                                            [two] => 2
                                                            [three] => 1
                                                        )

                                                )

                                        )

                                    [EU] => testcaseB Object
                                        (
                                            [one] => 4
                                            [two] => 2
                                            [three] => 1
                                            [obj] => Array
                                                (
                                                    [France] => testcaseB Object
                                                        (
                                                            [one] => 3
                                                            [two] => 2
                                                            [three] => 1
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

这仍然不是我所追求的行为,但它更接近。我需要的是使用一个对象链来遍历树(与第一种情况一样),一个指向值 $two 和 $three 在实际情况下是数组而不是交换的指针。我不想做的是不必要地复制对象。

另一方面,我确实需要让所有对象共享他们都使用的一对变量。

我的猜测是 get() 方法可以使用 byval 而不是 byref 尽管本能地这似乎是错误的。

谁能解释$one 值在做什么?

还有谁能帮我理解第一个测试用例的行为,尤其是第一次对数组中的值的理解?

更新

利用很棒的建议,我们的测试用例现在看起来像这样:

class testcaseA {
    public function __construct($one=0, $two=1, $three=2) {
        $this->obj = new testcaseB(++$one, $three, $two);
    }
    public function &get($what){
        return $this->obj->get($what);
    } 
}
class testcaseB {
    public function __construct($one, &$two, &$three) {
        $this->one=$one;
        $this->two=$two;
        $this->three=$three;
        echo "[New:\$one={$one}]:";
    }
    //public function &get($what){
    public function &get($what){
        //echo "<p>You asked for $what. $what ain't no country I ever heard of.<br />";
        echo "Get:{$what}:[{$this->one}]<br />";
        if(!isset($this->obj[$what])){
            $this->obj[$what] = new testcaseB(++$this->one,$this->two,$this->three);
        }
        return $this->obj[$what];
    } 
}
echo "STARTING:<br />";
ini_set('display_errors',1); 
error_reporting(E_ALL);
echo "REALLY STARTING:<br />";
echo "<pre>";
echo "<p>One at a time:</p>";
$bob = new testcaseA();
$a=$bob->get("What");
$b=$a->get("Spam");
$c=$b->get("America");
$d=$a->get("EU");
$e=$d->get("France"); 
echo "<br />";
print_r($bob); 
echo "<p>Chained:</p>";
$bobby = new testcaseA();
$bobby->get("What")->get("Spam")->get("America");
$bobby->get("What")->get("EU")->get("France");
echo "<br />";
print_r($bob);

其中的输出是:

STARTING:
REALLY STARTING:

One at a time:
[New:$one=1]:Get:What:[1]
[New:$one=2]:Get:Spam:[2]
[New:$one=3]:Get:America:[3]
[New:$one=4]:Get:EU:[3]
[New:$one=4]:Get:France:[4]
[New:$one=5]:
testcaseA Object
(
    [obj] => testcaseB Object
        (
            [one] => 2
            [two] => 2
            [three] => 1
            [obj] => Array
                (
                    [What] => testcaseB Object
                        (
                            [one] => 4
                            [two] => 2
                            [three] => 1
                            [obj] => Array
                                (
                                    [Spam] => testcaseB Object
                                        (
                                            [one] => 4
                                            [two] => 2
                                            [three] => 1
                                            [obj] => Array
                                                (
                                                    [America] => testcaseB Object
                                                        (
                                                            [one] => 4
                                                            [two] => 2
                                                            [three] => 1
                                                        )

                                                )

                                        )

                                    [EU] => testcaseB Object
                                        (
                                            [one] => 5
                                            [two] => 2
                                            [three] => 1
                                            [obj] => Array
                                                (
                                                    [France] => testcaseB Object
                                                        (
                                                            [one] => 5
                                                            [two] => 2
                                                            [three] => 1
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

Chained:
[New:$one=1]:Get:What:[1]
[New:$one=2]:Get:Spam:[2]
[New:$one=3]:Get:America:[3]
[New:$one=4]:Get:What:[2]
Get:EU:[3]
[New:$one=4]:Get:France:[4]
[New:$one=5]:
testcaseA Object
(
    [obj] => testcaseB Object
        (
            [one] => 2
            [two] => 2
            [three] => 1
            [obj] => Array
                (
                    [What] => testcaseB Object
                        (
                            [one] => 4
                            [two] => 2
                            [three] => 1
                            [obj] => Array
                                (
                                    [Spam] => testcaseB Object
                                        (
                                            [one] => 4
                                            [two] => 2
                                            [three] => 1
                                            [obj] => Array
                                                (
                                                    [America] => testcaseB Object
                                                        (
                                                            [one] => 4
                                                            [two] => 2
                                                            [three] => 1
                                                        )

                                                )

                                        )

                                    [EU] => testcaseB Object
                                        (
                                            [one] => 5
                                            [two] => 2
                                            [three] => 1
                                            [obj] => Array
                                                (
                                                    [France] => testcaseB Object
                                                        (
                                                            [one] => 5
                                                            [two] => 2
                                                            [three] => 1
                                                        )

                                                )

                                        )

                                )

                        )

                )

        )

)

输出数字似乎正确,但 $one 在堆栈中已关闭。

【问题讨论】:

  • 有趣的是,当 get 成为正常的 byval 函数时,我没有得到预期的行为。
  • 以另一种顺序运行两个版本(一个接一个和链)仍然有链对对象堆栈做坏事。是的,我现在已经知道 $i++ 与 ++$i 不同。

标签: php testing pointers byref


【解决方案1】:

如果我了解您对什么感到困惑(并且我认为我确实),那么您已经遇到了pre- and post-increment 之间的细微差别。这可以很容易地在代码中演示:

$a = 1;
echo $a++; // 1
echo $a;   // 2

另一方面:

$a = 1;
echo ++$a; // 2
echo $a;   // 2

基本上,通过将++ 放在您增加的值之前,您将获得新值。通过将其放置在(如您所做的那样)之后,您将获得旧值。

我认为您的代码中的关键行是这样的:

$this->obj[$what] = new testcaseB($this->one++,$this->two,$this->three);

...应该是这样的:

$this->obj[$what] = new testcaseB(++$this->one,$this->two,$this->three);

通过后自增,$this-&gt;one 的初始值将通过所有后续迭代进行。

顺便说一句,我认为您不必担心在这里通过引用返回,因为 PHP5 中的所有对象无论如何都是通过引用传递的。

【讨论】:

  • 这解释了价值观做自己的事情。将来我一定会注意这一点。让我真正陷入困境的是对象 $bob 不是一棵树——首先缺少“垃圾邮件”和“美国”。
  • @MatthewBrown 好的,让我再看看。
  • 非常感谢。在我的原始(非平凡示例)中,$this-&gt;value 似乎不存在于名为&amp;get($child) 的方法中,但在所有其他地方都可用。 $one++ 只是为了代表这一点(令人尴尬的是我错过了一个细微的差别)。
  • @MatthewBrown 好的,我想通了。问题线实际上与我上面提到的相同。因为您在创建新对象之前没有检查$this-&gt;obj[$what] 是否已经存在,所以您正在覆盖上一次迭代中的实例。解决此问题所需要做的就是在行前加上if (!isset($this-&gt;obj[$what]))。 PS我喜欢名字像$bob的变量
  • 这也有助于解释测试用例的行为。但是:$bobby-&gt;get("What")-&gt;get("Spam")-&gt;get("America"); $bobby-&gt;get("What")-&gt;get("EU")-&gt;get("France"); 在调用 get("What") 时产生 1 和 3。这并不是说我的测试用例是垃圾,你的回答并不正确。
猜你喜欢
  • 2021-12-28
  • 1970-01-01
  • 1970-01-01
  • 2019-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多