PHP 中的父级::父级

发布于 2024-12-17 08:46:46 字数 682 浏览 5 评论 0原文

我搜索一种方法来访问类的父类、父函数而不调用父类...嗯,这听起来有点奇怪,所以我将给出一个例子:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

掐死时间 2024-12-24 08:46:46

简单的解决方案。直接使用根对象 myclass:

class myclass3 extends myclass2
{
  public function test() { return myclass::test() . '-level 3'; }
}

如果您需要更通用的方法,请查看 outis 答案

Simple solution. Use the root object myclass directly:

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.

━╋う一瞬間旳綻放 2024-12-24 08:46:46

您可以使用 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'; 
    }
}

或者您可以使用 反射

function get_grandparent_class($thing) {
    if (is_object($thing)) {
        $thing = get_class($thing);
    }
    $class = new ReflectionClass($thing);
    return $class->getParentClass()->getParentClass()->getName();
}

但是,这可能不是一个好主意,具体取决于您想要实现的目标。

You could do it using 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.

那些过往 2024-12-24 08:46:46

也许您可以将 myclass2 添加为 myclass3 中的成员对象,然后尝试编写如下代码:

class myclass3{
myclass2 obj2;
public function test() { return $obj2->callParentTest() . '-level3';}
}

Maybe you can just add myclass2 as a member object in myclass3 and try to code like :

class myclass3{
myclass2 obj2;
public function test() { return $obj2->callParentTest() . '-level3';}
}
十级心震 2024-12-24 08:46:46

不,这是不可能的。不幸的是,无法直接引用原始类,只能引用它的 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's parent.

花开柳相依 2024-12-24 08:46:46

您不能链接父类,而是在父类中创建某种仅返回 $this; 的 GetParent() 方法。

You cannot chain parents, instead create some sort of GetParent() method in your parent classes that simply returns $this;

叶落知秋 2024-12-24 08:46:46

如果您想直接在 class1 上使用测试函数,则必须从 class1 扩展。请搜索多态性。

当你有 class5 时,你会尝试“parent::parent::parent::parent”吗?

我认为您可以在测试方法中添加一个 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"

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.

<?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"
攒眉千度 2024-12-24 08:46:46

没有运算符可以获取根对象。我会做这样的事情:

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"

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"
朮生 2024-12-24 08:46:46

在某些情况下,您可能可以完全覆盖根的方法。我的意思是,您可以复制父级的父级的方法代码并添加您的方法代码,而不是调用父级的父级。

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.

浪漫人生路 2024-12-24 08:46:46

我的方法:
如果你有代码:

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');
    }
}

并且您希望在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();
    }
}

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 method from class A (but not class B) refactor it to the code:

<?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();
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文