PHP 中的父 :: 父
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8212048/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
parent::parent in PHP
提问by Alexandre
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:
我想显示“level 1-level 3”然后做这样的事情:
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...)
你知道我该怎么做吗?(我不允许编辑 myclass 和 myclass2,它们是框架的一部分......)
回答by PiTheNumber
Simple solution. Use the root object myclass directly:
简单的解决方案。直接使用根对象myclass:
class myclass3 extends myclass2
{
public function test() { return myclass::test() . '-level 3'; }
}
If you need a more general approach have a look at outis answer.
如果您需要更通用的方法,请查看outis answer。
回答by outis
You could do it using get_parent_class
你可以用 get_parent_class
function get_grandparent_class($thing) {
if (is_object($thing)) {
$thing = get_class($thing);
}
return get_parent_class(get_parent_class($thing));
}
class myclass3 extends myclass2 {
public function test() {
$grandparent = get_grandparent_class($this);
return $grandparent::test() . '-level 3';
}
}
Or you could use reflection:
或者你可以使用反射:
function get_grandparent_class($thing) {
if (is_object($thing)) {
$thing = get_class($thing);
}
$class = new ReflectionClass($thing);
return $class->getParentClass()->getParentClass()->getName();
}
However, it may not be a good idea, depending on what you're trying to achieve.
但是,这可能不是一个好主意,这取决于您要实现的目标。
回答by Wesley van Opdorp
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's parent
.
不,这是不可能的。不幸的是,无法直接引用原始类,只能引用 it'sself
或 it's parent
。
回答by LostMohican
Maybe you can just add myclass2 as a member object in myclass3 and try to code like :
也许您可以将 myclass2 添加为 myclass3 中的成员对象并尝试编写如下代码:
class myclass3{
myclass2 obj2;
public function test() { return $obj2->callParentTest() . '-level3';}
}
回答by Eric Herlitz
You cannot chain parents, instead create some sort of GetParent() method in your parent classes that simply returns $this;
你不能链接父母,而是在你的父类中创建某种 GetParent() 方法,它只返回 $this;
回答by Tufan Bar?? Y?ld?r?m
if you want use the test function directly on class1 you must extend from class1. Please search about polimorphism.
如果你想直接在 class1 上使用测试函数,你必须从 class1 扩展。请搜索多态性。
will you try "parent::parent::parent::parent
" when you have class5 ??
parent::parent::parent::parent
当你有第5课时,你会尝试“ ”吗??
I think you can add a level
parameter to test method. and check it first.
我认为您可以level
向测试方法添加一个参数。并先检查一下。
<?php
class myclass
{
public function test($level)
{
return 'level 1';
}
}
class myclass2 extends myclass
{
public function test($level)
{
return $level >= 2 ? parent::test($level) . '-level 2' : parent::test($level);
}
}
class myclass3 extends myclass2
{
public function test()
{
return parent::test(1) . '-level 3';
}
}
$example = new myclass3();
echo $example->test(); // should display "level 1-level 3"
回答by Luca Reghellin
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.
在某些情况下,您可能可以完全覆盖 root 的方法。我的意思是,您可以复制父母的方法代码并添加您的方法代码,而不是调用父母的父母的方法代码。
回答by W92
my approach: if you have the code:
我的方法:如果你有代码:
class A {
protected function method () {
var_dump('A -> method');
}
}
class B extends A {
protected function method () {
var_dump('B -> method');
}
}
class C extends B {
protected function method () {
var_dump('C -> method');
}
}
and you want in class C
call methodfrom class A
(but not class B
) refactor it to the code:
并且您希望在class C
调用方法中class A
(但不是class B
)将其重构为代码:
<?php
class A {
protected function method () {
$this->methodA();
}
protected function methodA () {
var_dump('A -> method');
}
}
class B extends A {
protected function method () {
var_dump('B -> method');
}
}
class C extends B {
protected function method () {
$this->methodA();
}
}
回答by PiTheNumber
There is no operator to get the root object. I would do something like this:
没有获取根对象的运算符。我会做这样的事情:
class myclass
{
public function getRoot() { return __CLASS__; }
public function test() { return 'level 1'; }
}
class myclass2 extends myclass
{
public function getRoot() { return parent::getRoot(); }
}
class myclass3 extends myclass2
{
public function getRoot() { return parent::getRoot(); }
public function test() {
$grandparent = self::getRoot();
return $grandparent::test() . '-level 3';
}
}
$example = new myclass3();
echo $example->test(); // should display "level 1-level 2-level 3"