PHP 和类:访问父类中父类的公共属性
这是我的代码,
我有两种形式:
class Form_1 extends Form_Abstract {
public $iId = 1;
}
class Form_2 extends Form_1 {
public $iId = 2;
}
我希望代码的行为如下:
$oForm = new Form_2;
echo $oForm->getId(); // it returns '2'
echo $oForm->getParentId(); // i expect it returns '1'
这是我的 Form_Abstract 类:
class Form_Abstract {
public $iId = 0;
public function getId() {
return $this->iId;
}
/**
this method will be called from a child instance
*/
public function getParentId() {
return parent::$iId;
}
}
但它抛出一个致命错误:
Fatal error: Cannot access parent:: when current class scope has no parent
请帮助我使用方法 getParentId()
< strong>PS:我知道发生这种情况的原因,我正在寻找解决方案。
here is what my code looks like
i have two forms:
class Form_1 extends Form_Abstract {
public $iId = 1;
}
class Form_2 extends Form_1 {
public $iId = 2;
}
i expect the code behave like this:
$oForm = new Form_2;
echo $oForm->getId(); // it returns '2'
echo $oForm->getParentId(); // i expect it returns '1'
here is my Form_Abstract class:
class Form_Abstract {
public $iId = 0;
public function getId() {
return $this->iId;
}
/**
this method will be called from a child instance
*/
public function getParentId() {
return parent::$iId;
}
}
but it throws a Fatal Error:
Fatal error: Cannot access parent:: when current class scope has no parent
please help me with the method getParentId()
PS: i know the reason of what happens, i am seeking for the solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您必须使用Reflection Api来访问父类的属性默认值。用这个替换
Form_Abstract
中的 getParentId,一切正常:显然你不能在根类中调用 getParentId(),所以最好检查父类是否存在。
UDATE:
您可以对类/对象函数执行相同的操作:
You have to use Reflection Api to access the parent class' property default value. Substitute getParentId, in
Form_Abstract
, with this, and all works fine:Clearly you cannot call getParentId() in the root class, so it's better to check if a parent class exists.
UDATE:
You can do the same with classes/objects functions:
该错误是因为您正在调用没有父级的类的父级(它不扩展现有类)。
The error is because you are calling the parent of a class that does not have a parent (it does not extend an existing class).
我认为甚至不可能访问“父”版本的
$iId
:您实际上并没有在子类中重新定义它:您只有机会获得原来的值在父类中定义。让事情变得非常简单:当您声明
扩展 Form_1
的Form_2
类时,它会获取Form_2
的所有属性和方法,并将它们放入在Form_1
中,覆盖那里已经存在的内容。不再存在“两个不同的类”:只有一个结果对象,即同时是
Form_1
和Form_2
。这是一个我希望能够帮助理解我的意思的例子:
使用这部分代码,调用
$form2->methodInParent()
将会得到:即即使调用/执行在父类中定义的方法,
$iId< /code> 属性仍然是子类中定义的值:该属性只有一个版本!
调用
$form2->tryingToGetParentProperty()
将会得到:因为
中没有名为
。$iId
的static
属性表格_1我想避免这种情况的解决方案是将
$iId
声明为static
——但请注意,这会改变代码的含义及其行为方式!即
static
变量将在类的所有实例中共享——这可能不是您想要的 ^^I don't think it is even possible to access the "parent"'s version of
$iId
: you don't actually re-define it in the child class : you only chance the value that was defined in the parent's class.To makes things very simple : when you declare the
Form_2
class thatextends Form_1
, it takes all the properties and methods ofForm_2
, and put them inForm_1
, overriding what was already existing there.There is no longer "two distinct classes" : there is only one resulting object, that's both
Form_1
andForm_2
at the same time.And here's an example that kind of -- I hope -- will help understand what I mean :
Using this portion of code, the call to
$form2->methodInParent()
will get you :i.e. even if calling/executing a method that's defined in the parent's class, the
$iId
property is still the value defined in the child class : there is one, and only one, version of that property !And the call to
$form2->tryingToGetParentProperty()
will get you :As there is no
static
property called$iId
inForm_1
.I suppose a solution to avoid that situation would be to declare
$iId
asstatic
-- but note that it would change the meaning of your code, and the way it behaves !i.e. the
static
variable will be shared accross all instances of the class -- which is probably not what you want ^^我是这样工作的:
但是它是一个标准解决方案吗,它是否有任何性能问题?
i made it work by this:
BUT is it a standard solution, does it have any performance issue?