PHP 中的父级::父级
我搜索一种方法来访问类的父类、父函数而不调用父类...嗯,这听起来有点奇怪,所以我将给出一个例子:
class myclass
{
public function test() { return 'level 1'; }
}
class myclass2 extends myclass
{
public function test() { return parent::test() . '-level 2'; }
}
class myclass3 extends myclass2
{
public function test() { return parent::test() . '-level 3'; }
}
$example = new myclass3();
echo $example->test(); // should display "level 1-level 2-level 3"
我想显示“level 1-level 3”然后做类似的事情:
class myclass3 extends myclass2
{
public function test() { return parent::parent::test() . '-level 3'; }
}
你知道我该怎么做吗? (我不允许编辑 myclass 和 myclass2,它们是框架的一部分......)
I search a way to access to the parent, parent function of a class without to call the parent... Hmmm, that sound a bit weird explanation so i will give an example:
class myclass
{
public function test() { return 'level 1'; }
}
class myclass2 extends myclass
{
public function test() { return parent::test() . '-level 2'; }
}
class myclass3 extends myclass2
{
public function test() { return parent::test() . '-level 3'; }
}
$example = new myclass3();
echo $example->test(); // should display "level 1-level 2-level 3"
I would like to display "level 1-level 3" then doing something like that:
class myclass3 extends myclass2
{
public function test() { return parent::parent::test() . '-level 3'; }
}
Do you have an idea how I can do this? (I am not allow to edit myclass and myclass2, they are part of a framework...)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
简单的解决方案。直接使用根对象 myclass:
如果您需要更通用的方法,请查看 outis 答案。
Simple solution. Use the root object myclass directly:
If you need a more general approach have a look at outis answer.
您可以使用
get_parent_class
或者您可以使用 反射:
但是,这可能不是一个好主意,具体取决于您想要实现的目标。
You could do it using
get_parent_class
Or you could use reflection:
However, it may not be a good idea, depending on what you're trying to achieve.
也许您可以将 myclass2 添加为 myclass3 中的成员对象,然后尝试编写如下代码:
Maybe you can just add myclass2 as a member object in myclass3 and try to code like :
不,这是不可能的。不幸的是,无法直接引用原始类,只能引用它的
self
或其parent
。No, this is not possible. Unfortunately there is no possibility to refer directly to the original class, only to it's
self
or to it'sparent
.您不能链接父类,而是在父类中创建某种仅返回 $this; 的 GetParent() 方法。
You cannot chain parents, instead create some sort of GetParent() method in your parent classes that simply returns $this;
如果您想直接在 class1 上使用测试函数,则必须从 class1 扩展。请搜索多态性。
当你有 class5 时,你会尝试“
parent::parent::parent::parent
”吗?我认为您可以在测试方法中添加一个
level
参数。并先检查一下。if you want use the test function directly on class1 you must extend from class1. Please search about polimorphism.
will you try "
parent::parent::parent::parent
" when you have class5 ??I think you can add a
level
parameter to test method. and check it first.没有运算符可以获取根对象。我会做这样的事情:
There is no operator to get the root object. I would do something like this:
在某些情况下,您可能可以完全覆盖根的方法。我的意思是,您可以复制父级的父级的方法代码并添加您的方法代码,而不是调用父级的父级。
In some situations, probably you can entirely override the root's method. I mean, instead of calling the parent's parent's one, you can copy the parent's parent's method code and add yours.
我的方法:
如果你有代码:
并且您希望在
class C
中调用class A
中的方法(但不是class B
)将其重构为代码:my approach:
if you have the code:
and you want in
class C
call method fromclass A
(but notclass B
) refactor it to the code: