简体   繁体   English

PHP子类属性继承

[英]PHP Child Class Property Inheritance

I have a base class called Parent . 我有一个叫做Parent的基类。 Within parent I am setting a class property, $this->admin = TRUE ; 在父级中,我正在设置一个类属性, $this->admin = TRUE

I also have a child class we'll call Child . 我还有一个孩子班,我们称之为Child The constructor for Child calls the parent constructor. Child的构造函数调用父构造函数。 I was under the impression that when you set a class property int he parent class, that property is inherited by the child, unless you override it. 我的印象是,当您在父类中设置一个类属性时,该属性将由子级继承,除非您将其覆盖。 yet when I test to see if $this->admin is set I get false. 但是当我测试以查看是否设置了$this->admin ,我得到了false。 Did I get the concept of inheritance wrong? 我对继承的概念有误吗?

不能使用$ this-> prop来访问类属性 ,而只能使用self :: $ prop来访问该属性,还应确保该属性未设置为private而是设置为publicprotected以便将其继承给子类。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM