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

)我有OO和php的基本知识,但我无法弄清楚当private被阻止时(prop_2)被调用(或显示?)的是什么?它不能是一个私人/公共/受保护的问题,因为'prop_1'是私人的,可以被调用并显示......对吗?

这是一个问题,在子元素类与父类分配值?

我很感谢帮助理解原因。

谢谢。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值