PHP:如何在子构造中调用父构造的私有值?
我希望能够在父构造函数中设置私有属性的值,并在子构造函数或方法中调用该值。
例如:
<?php
abstract class MainClass
{
private $prop_1;
private $prop_2;
function __construct()
{
$this->prop_2 = 'this is the "prop_2" property';
}
}
class SubClass extends MainClass
{
function __construct()
{
parent::__construct();
$this->prop_1 = 'this is the "prop_1" property';
}
public function GetBothProperties()
{
return array($this->prop_1, $this->prop_2);
}
}
$subclass = new SubClass();
print_r($subclass->GetBothProperties());
?>
输出:
Array
(
[0] => this is the "prop_1" property
[1] =>
)
但是,如果我将 prop_2
更改为 protected
,输出将是:
Array
(
[0] => this is the "prop_1" property
[1] => this is the "prop_2" property
)
I have basic Knowledge of OO and php, but我不明白是什么阻止 prop_2
在 private
时被调用(或显示?);它不可能是私有/公共/受保护的问题,因为“prop_1”是私有的并且能够被调用和显示......对吗?
这是子类与父类中赋值的问题吗?
我希望能帮助您理解原因。
谢谢。
I want to be able to set a private attribute's value in the parent constructor, and call the value in a child's constructor or method.
For example:
<?php
abstract class MainClass
{
private $prop_1;
private $prop_2;
function __construct()
{
$this->prop_2 = 'this is the "prop_2" property';
}
}
class SubClass extends MainClass
{
function __construct()
{
parent::__construct();
$this->prop_1 = 'this is the "prop_1" property';
}
public function GetBothProperties()
{
return array($this->prop_1, $this->prop_2);
}
}
$subclass = new SubClass();
print_r($subclass->GetBothProperties());
?>
Output:
Array
(
[0] => this is the "prop_1" property
[1] =>
)
However, if I change prop_2
to protected
, the output will be:
Array
(
[0] => this is the "prop_1" property
[1] => this is the "prop_2" property
)
I have basic knowledge of OO and php, but I can't figure out what is preventing prop_2
from being called (or shown?) when it's private
; it can't be a private/public/protected issue, since 'prop_1' is private and able to be called and shown... right?
Is it an issue of assigning the values in the child class vs parent class?
I would appreciate help in understanding why.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
子类中不能访问父类的私有属性,反之亦然。
你可以这样做
Private properties of parent class can not be accessed in Child class and vice versa.
You can do like this
如果要从子类访问父类的属性,则必须将父类的属性设置为 protected NOT private。这样它们仍然无法从外部访问。
您无法按照您尝试的方式在子类中覆盖父级的私有属性可见性。
If you want to access the parent's properties from the child class, you must make the parent's properties protected NOT private. This way they are still inaccessible externally.
You can't override the parent's private properties visibility in the child class in the way you are trying to.
正如其他人所指出的,您需要将父级的属性更改为“受保护”。但是,另一种方法是为您的父类实现一个
get
方法,该方法允许您访问该属性,或者如果您希望能够访问该属性,则实现一个set
方法。 -骑它。因此,在您的父类中,您需要声明:
然后,在您的子类中,您可以访问
$this->setProp1("someval");
和$val = $this ->getProp1()
,分别。As others have noted, you'd need to change the parent's properties to
protected
. However, the other way is by implementing aget
method for your parent class, which allows you access to the property, or implementing aset
method if you want the ability to over-ride it.So, in your parent class, you'd declare:
Then, in your child class, you can access
$this->setProp1("someval");
and$val = $this->getProp1()
, respectively.我在这里找到了一个与 lambda 一起使用的简单技巧: https ://www.reddit.com/r/PHP/comments/32x01v/access_private_properties_and_methods_in_php_7/
基本上,您使用 lambda 并将其绑定到实例,然后您可以访问它的私有方法和属性
有关 lambda 调用的信息: https://www.php.net/manual/en/closure.call。 php
There is a simple trick you an use with lambdas, which I found here: https://www.reddit.com/r/PHP/comments/32x01v/access_private_properties_and_methods_in_php_7/
basically you use a lambda and bind it to the instance and then you can access it's private methods and properties
Info on the lambda call: https://www.php.net/manual/en/closure.call.php