简体   繁体   English

为什么我可以在父PHP类中访问私有财产?

[英]Why I can access private property in parent php class?

We recently found this strange PHP behaviour. 我们最近发现了这种奇怪的PHP行为。 Accessing a private in the parent class should not work. 在父类中访问私有资源不起作用。 Is this a feature? 这是功能吗? Maybe someone can explain it. 也许有人可以解释。

// PHP classes

class Father {
    // private property
    private $value = 'test';
}

Class Child extends Father {

    // Should fail, se
    public function setValue() {
     $this->value = 'why does';
    }

    public function getValue() {
     return $this->value;
    }
}


$c = new Child();

// should fail!
$c->setValue();
echo $c->getValue() . "|";

// should fail!!!!!!!
$c->value = "it work?";
echo $c->getValue();

// output: why does|it work?

您不是要更改父亲的私有财产,而是要为孩子引入新的财产value

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 php类私有属性访问类外 - php class private property access outside class PHP无法访问类中的私有属性 - PHP Cannot access private property inside class PHP-为什么我可以在此处打印私有财产? - PHP - Why can I print a private property here? 构造函数可以访问PHP中的父私有属性吗? - Constructor can access parent private properties in PHP? 如何在PHP中的Overloading(使用__get()魔术方法或其他方法)类之外访问现有的私有属性? - How can I access an existing private property out of the class in Overloading(using __get() magic method or some other way) in PHP? PHP继承的父方法无法访问子属性的私有属性 - PHP Inherited parent method can't access child's private property PHP:为什么子类的继承方法访问父级的私有属性? - PHP: Why do Inherited Methods of Child Class access Parent's Private Properties? 为什么我不能在PHP的类中声明私有变量? - Why I can't declare a private variable in a class in PHP? 为什么可以在课堂之外访问私有财产? - Why can a private property be accessed outside the class? 父 class 中的私有属性可以通过 PHP 中的子 class 的实例访问和重新分配,但受保护不能 - Private property in parent class can be accessed and re-assigned through the instance of child class in PHP but protected one can't
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM