从声明它自己的 foo 的扩展类调用 foo 的第一个声明

发布于 2024-09-29 02:24:56 字数 1097 浏览 0 评论 0原文

<?php
class a {
    public function foo() {
        echo __METHOD__ . PHP_EOL;
    }
}
class b extends a {
    public function foo() {
        $this->foo(); # I want to call a's foo method here.
        echo __METHOD__ . PHP_EOL;
    }
}
class c extends a {
    public function foo() {
        echo __METHOD__ . PHP_EOL;
        $this->foo(); # I want to call a's foo method here.
    }
}
class d extends a {
    # This class does not handle anything super special in our foo step, so I don't over write a's foo();
}
$a = new a; $a->foo();
$b = new b; $b->foo();
$c = new c; $c->foo();
$d = new d; $d->foo();
?>

我确信你们中的一些人挠头试图弄清楚为什么我会想做这样的事情,所以我向你们保证这是有道理的,让我解释一下。

我有一个可以自行正常工作的函数,并且我有一些其他类在基类上扩展以向其添加一些函数,这些类执行相同的任务,但在不同的上下文中或具有附加信息,因此它们必须执行所有操作父类的步骤,但在某些情况下它们还必须执行一些特殊的子集。

有没有办法我仍然可以执行 a 的 foo 方法和我的子类 foo 方法?

我知道我可以通过一些可怕的技巧来解决这个问题,例如将该方法制作为一个文件,然后将该文件包含在父函数及其兄弟函数中(如果需要),或者我可以制作第二个名为 bar() 的方法,其中包含以下指令foo() 并让我的子类覆盖 foo() 但在需要 foo() 的功能时调用 bar() 。我只是想知道我是否可以在不使用一些非常有趣的技巧的情况下做到这一点,是否有一些关于 PHP 的我不知道的事情,这里有人可以教我?

<?php
class a {
    public function foo() {
        echo __METHOD__ . PHP_EOL;
    }
}
class b extends a {
    public function foo() {
        $this->foo(); # I want to call a's foo method here.
        echo __METHOD__ . PHP_EOL;
    }
}
class c extends a {
    public function foo() {
        echo __METHOD__ . PHP_EOL;
        $this->foo(); # I want to call a's foo method here.
    }
}
class d extends a {
    # This class does not handle anything super special in our foo step, so I don't over write a's foo();
}
$a = new a; $a->foo();
$b = new b; $b->foo();
$c = new c; $c->foo();
$d = new d; $d->foo();
?>

I'm sure there are a few of you scratching your head trying to figure out why I would ever want to do something like this, so I promise you this will make sense, just let me explain.

I have a function that works just fine by it's self and I have some other classes that extend upon the base class to add some functions to it, these classes do the same task but in a different context or with additional information so they must do all of the steps of the parent class but they must also do some special sub set's in some cases.

Is there a way I can still execute a's foo method and my sub classes foo method?

I know I could fix this with some awful hacks like making the method a file, and then including that file in both the parent function and it's siblings where needed, or I could make a second method called bar() that would contain the instructions of foo() and have my sub classes overwrite foo() but call bar() when they need the functionality of foo(). I'm just wondering if I can do this without using some pretty interesting hacks, is there something I don't know about PHP that someone on here can teach me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

嗳卜坏 2024-10-06 02:24:56

你可以尝试parent::foo();

在 c 类中:

public function bar(){
  return parent::foo();
}

请参阅 PHP 手册中有关范围解析的示例 3

You could try parent::foo();

in the c class:

public function bar(){
  return parent::foo();
}

See Example 3 in the PHP Manual on Scope Resolution

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