奇怪的php oop行为,有人可以解释吗?

问题描述

| 简而言之:一个类从其父类继承一个函数。然后,该函数在子级上被调用,但似乎仍具有父类的范围。这是预期的行为吗? 考虑以下代码示例:
<?PHP
class OLTest {
    private $var1 = 10;

    public function getvar1() {
        if (isset($this->var1)) {
            return $this->var1;
        } else {
            return \'undefined\';
        }
    }

    public function getvar2() {
        if (isset($this->var2)) {
            return $this->var2;
        } else {
            return \'undefined\';
        }
    }
}

class OLTest2 extends OLTest {
    private $var1 = 11;
    private $var2 = 20;
}

$oltest = new OLtest();
$oltest2 = new OLTest2();

echo \"calling parent->getvar1\\n\";
echo $oltest->getvar1() . \"\\n\";

echo \"calling parent->getvar2\\n\";
echo $oltest->getvar2() . \"\\n\";

echo \"calling child->getvar1\\n\";
echo $oltest2->getvar1() . \"\\n\";

echo \"calling child->getvar2\\n\";
echo $oltest2->getvar2() . \"\\n\";
?>
据我了解,输出应为:
calling parent->getvar1
10
calling parent->getvar2
undefined
calling child->getvar1
11
calling child->getvar2
20
我的机器上的实际输出为:
calling parent->getvar1
10
calling parent->getvar2
undefined
calling child->getvar1
10
calling child->getvar2
undefined
更令人困惑的是,两个函数中的一个“ 3”将表明作用域确实设置为子类,但无法访问该变量。 有人可以帮我清理一下吗? 编辑:我在版本5.3.3-1ubuntu9.5中使用PHP。     

解决方法

        不,输出应明确为
10,undefined,10,undefined
。 原因是
private
的变量仅对定义它们的类(而不是上级或子类)可见。 因此,当调用
child
时,其方法在父对象中定义,并且当它们解析
var1
或check8ѭ时,将检查为
OLTest
定义的范围。 the8ѭ也无法访问,因为声明的
var2
仅在
OLTest2
内部可见。 为了获得输出,您应该声明这些变量
protected
public
。     ,        它似乎与“ 5”作用域有关。 尝试更改为
protected
public
,然后检查结果。     ,        是的,这是因为您的财产是私人的。您正在调用的方法仍属于父类,因为您尚未重新定义它们。要使父方法能够访问子级的属性,必须在父类和子类中将它们设置为“ 13”:
class OLTest {
    protected $var1 = 10;

    public function getVar1() {
        if (isset($this->var1)) {
            return $this->var1;
        } else {
            return \'undefined\';
        }
    }

    public function getVar2() {
        if (isset($this->var2)) {
            return $this->var2;
        } else {
            return \'undefined\';
        }
    }
}

class OLTest2 extends OLTest {
    protected $var1 = 11;
    protected $var2 = 20;
}
如果在父类中将其保留为“ 5”,则它将不允许子类覆盖该属性,因此属于父类的函数将访问其自己的私有属性。而且,如果您在子类中将其设为私有,则不允许父方法访问它。 如果仍然想让它们保密,则必须将方法的代码复制粘贴到子类中(是的,
return parent::getVar1()
也不起作用)。     ,        您不能“ 22”“插入”父类,这是您试图做的。 是的,这是正常行为。     ,        因为您已将自己的var声明为私有,所以它们完全相互隐藏。因此,父母中的
$var1
实际上是与孩子中的
$var1
不同的变量 当您在父级上调用
getVar1
时,它将使用它看到的
$var1
,即来自父级的值为23的那个。类似地,父级无法看到子级的
$var2
,因此该属性未定义。 当在子级上调用
getVar1
getVar2
时,方法本身是从父类继承的,其行为与从OLTest类型的对象中调用它们时的行为完全相同。     ,        这是您的私有变量的范围,请尝试使用受保护的
class OLTest {
    protected $var1 = 10;

    public function getVar1() {
        return isset($this->var1) ? $this->var1:\'undefined\';
    }

    public function getVar2() {
        return isset($this->var2) ? $this->var2:\'undefined\';
    }
}

class OLTest2 extends OLTest {
    protected $var1 = 11;
    protected $var2 = 20;
}

$oltest  = new OLTest();
$oltest2 = new OLTest2();

echo \"calling parent->getVar1\\n\".$oltest->getVar1().\"\\n\";
echo \"calling parent->getVar2\\n\".$oltest->getVar2().\"\\n\";
echo \"calling child->getVar1\\n\".$oltest2->getVar1().\"\\n\";
echo \"calling child->getVar2\\n\".$oltest2->getVar2().\"\\n\";
结果:
calling parent->getVar1
10
calling parent->getVar2
undefined
calling child->getVar1
11
calling child->getVar2
20