重新定义 PHP 方法,可以调用旧方法

发布于 2024-12-27 09:23:07 字数 141 浏览 2 评论 0原文

我有 method1() 对象;

例如,如何将 method1 移动到 method1_origin() 并创建具有附加功能和调用 method1_origin() 的可能性的新 method1?

我认为反思会有所帮助,但我不知道如何。

I have object with method1();

How can I move method1 for example to method1_origin() and create new method1 with additional functionality and possibility to call method1_origin()?

I think reflection can help, but I don't know how.

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

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

发布评论

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

评论(3

一个人的夜不怕黑 2025-01-03 09:23:07

您可以调用 parent 函数。

class new_object extends old_object
{
    function method1()
    {
        // overwrite function here
    }
    function method1_origin()
    {
        // call old function
        parent::method1();
    }
}

You can call a parent function.

class new_object extends old_object
{
    function method1()
    {
        // overwrite function here
    }
    function method1_origin()
    {
        // call old function
        parent::method1();
    }
}
云淡风轻 2025-01-03 09:23:07

基本的 OOP,您可以在这里找到一些好的信息: http://php.net/manual/ en/语言.oop5.php

Basic OOP, you can find some good info here: http://php.net/manual/en/language.oop5.php

天荒地未老 2025-01-03 09:23:07

你需要延长一堂课

class core { 

        public function __construct(){}


        protected function method_orig()
        {

        }
}

-

class someclass extends core{


       public function __construct()
       {
            parent::__construct();
       }

       public function method_new()
      {
          parent::method_orig(); 
      }
}

You need to extend a class

class core { 

        public function __construct(){}


        protected function method_orig()
        {

        }
}

-

class someclass extends core{


       public function __construct()
       {
            parent::__construct();
       }

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