需要在PHP中调用父类中的方法

发布于 2024-08-10 22:40:39 字数 381 浏览 5 评论 0原文

正如标题所述,我试图在父类中创建一个所需的方法。不过,我想它可以是任何班级。例如:

class Parent
   {
      function foo ()
        {
           // do stuff
        }
   }

  class Child extends Parent
    {
       function bar ()
        {
           // do stuff after foo() has ran
        }
    }

基本上,我希望 foo() 需要运行,否则 Child 类不会运行并返回错误或重定向到不同的页面。我可以调用该函数,但我想知道在扩展父类时是否可以将其作为一个要求。

As the title states, I'm trying to make a method in a parent class required. Although, I suppose it could be any class. For instance:

class Parent
   {
      function foo ()
        {
           // do stuff
        }
   }

  class Child extends Parent
    {
       function bar ()
        {
           // do stuff after foo() has ran
        }
    }

Basically, I want foo() to be required to run or Child class doesn't run and returns an error or redirects to a different page. I could call the function, but I'm wondering If I can make it a requirement when extending the parent class.

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

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

发布评论

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

评论(9

胡渣熟男 2024-08-17 22:40:39

如果您利用抽象类和方法,则可以强制子类实现缺少的方法。

abstract class ParentClass
{
  public function foo ()
  {
    // do stuff
    $this->bar();
  }

  abstract protected function bar();
}

class Child extends ParentClass
{
  protected function bar()
  {
    // does stuff
  }
}

未实现 bar() 的子类将生成致命错误。

If you leverage abstract classes and methods, you can force subclasses to implement the missing methods.

abstract class ParentClass
{
  public function foo ()
  {
    // do stuff
    $this->bar();
  }

  abstract protected function bar();
}

class Child extends ParentClass
{
  protected function bar()
  {
    // does stuff
  }
}

Subclasses that don't implement bar() will generate a fatal error.

浮世清欢 2024-08-17 22:40:39

您可能应该做的是重写 Parent::foo() ,然后在重写的方法中调用父方法,如下所示:

class Parent
{
  function foo ()
    {
       // do stuff
    }
}

class Child extends Parent
{
   function foo ()
    {
       if(!parent::foo()) {
            throw new Exception('Foo failed');
       }

       // do child class stuff
    }
}

What you should probably do is override Parent::foo() and then call the parent method in the overridden method like so:

class Parent
{
  function foo ()
    {
       // do stuff
    }
}

class Child extends Parent
{
   function foo ()
    {
       if(!parent::foo()) {
            throw new Exception('Foo failed');
       }

       // do child class stuff
    }
}
一腔孤↑勇 2024-08-17 22:40:39

为什么不在函数 foo() 中设置一个布尔值作为标志。检查它是否已在子类/函数中设置,一切都已设置。

Why not just set a boolean in function foo() that acts as a flag. Check to see if it has been set in the child class/functions, and you're all set.

痞味浪人 2024-08-17 22:40:39

让子级在构造中调用父级的函数。

class Child extends Parent
{
   function bar ()
    {
       // do stuff after foo() has ran
    }
    function __construct(){
         parent::foo();
    }
}

Have the child call the function from the parent in the construct.

class Child extends Parent
{
   function bar ()
    {
       // do stuff after foo() has ran
    }
    function __construct(){
         parent::foo();
    }
}
酒中人 2024-08-17 22:40:39

正如已经提到的,听起来您希望 foo() 是抽象的,强制子类覆盖它。

PHP 中任何包含抽象类的类都要求您的父类也是抽象的。这意味着它不能被实例化(构造),只能派生/子类化。如果您尝试实例化抽象类,编译器将发出致命错误。

http://php.net/manual/en/language.oop5.abstract。 php

请参阅 Peter Bailey 的答案中的代码。

As already mentioned, it sounds like you want foo() to be abstract, forcing child classes to override it.

Any class containing an abstract class in PHP requires your parent class to be abstract too. This means it can't be instantiated (constructed), only derived / sub-classed. If you try to instantiate an abstract class the compiler will issue a fatal error.

http://php.net/manual/en/language.oop5.abstract.php

See the code in Peter Bailey's answer.

沫雨熙 2024-08-17 22:40:39

如果您实际上没有初始化父类中的任何代码,您应该使用对象接口。必须实现接口方法,否则脚本将引发胎儿错误。

有关它们的更多信息,请访问:https://www.php.net/interface

If you're not actually initializing any code within parent class you should use an object interface. Interface methods have to be implemented or the script will throw a fetal error.

More information on them can be found: https://www.php.net/interface.

So尛奶瓶 2024-08-17 22:40:39

我认为这可能是实现此类功能的唯一方法,因为我认为没有内置的解决方案。

class Parent
{
    public $foo_accessed = FALSE;
    function foo ()
    {
       $this->foo_accessed=TRUE;
       // do stuff
    }
}

class Child extends Parent
{
   function bar ()
    {
       if($this->foo_accessed==TRUE) {
           // do stuff after foo() has ran
       } else {
           // throw an error
       }
    }
}

I think this might be the only way of implementing such functionality, as I don't think there is a built in solution.

class Parent
{
    public $foo_accessed = FALSE;
    function foo ()
    {
       $this->foo_accessed=TRUE;
       // do stuff
    }
}

class Child extends Parent
{
   function bar ()
    {
       if($this->foo_accessed==TRUE) {
           // do stuff after foo() has ran
       } else {
           // throw an error
       }
    }
}
泛泛之交 2024-08-17 22:40:39

不要依赖其他方法。确保他们已经跑了。

class Parent
{
    function foo()
   {
       // do stuff
   }
}

class Child extends Parent
{
    private function bar()
   {
       // do child class stuff
   }

   public function doFooBar()
   {
    parent::foo();
    $this->bar();
   }

}

Do not depend on other methods. Make sure they've ran.

class Parent
{
    function foo()
   {
       // do stuff
   }
}

class Child extends Parent
{
    private function bar()
   {
       // do child class stuff
   }

   public function doFooBar()
   {
    parent::foo();
    $this->bar();
   }

}
骷髅 2024-08-17 22:40:39

以下方法只会在所有处理完成后才会抱怨 - 但是,如果这对您公平的话,它肯定会确保 foo() 已在父类中调用,否则会触发您可以执行的条件采取行动。

class DemandingParent { 

    private $hasFooBeenCalled = false;

    public function foo() {
        $this->hasFooBeenCalled = true;

        /* do Stuff */
    }

    public function __destruct() {
        if (!$this->hasFooBeenCalled) {
            throw new Exception("Naughty Child! Call your parent's foo b4 you speak up!");
        }
    }
}

Following approach will only ever complain after all processing has been done - however if that is fair to you it will definately make sure foo() has been called in the parent class or otherwise trigger a condition that you can act upon.

class DemandingParent { 

    private $hasFooBeenCalled = false;

    public function foo() {
        $this->hasFooBeenCalled = true;

        /* do Stuff */
    }

    public function __destruct() {
        if (!$this->hasFooBeenCalled) {
            throw new Exception("Naughty Child! Call your parent's foo b4 you speak up!");
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文