需要在PHP中调用父类中的方法
正如标题所述,我试图在父类中创建一个所需的方法。不过,我想它可以是任何班级。例如:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
如果您利用抽象类和方法,则可以强制子类实现缺少的方法。
未实现
bar()
的子类将生成致命错误。If you leverage abstract classes and methods, you can force subclasses to implement the missing methods.
Subclasses that don't implement
bar()
will generate a fatal error.您可能应该做的是重写 Parent::foo() ,然后在重写的方法中调用父方法,如下所示:
What you should probably do is override Parent::foo() and then call the parent method in the overridden method like so:
为什么不在函数 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.
让子级在构造中调用父级的函数。
Have the child call the function from the parent in the construct.
正如已经提到的,听起来您希望 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.
如果您实际上没有初始化父类中的任何代码,您应该使用对象接口。必须实现接口方法,否则脚本将引发胎儿错误。
有关它们的更多信息,请访问: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.
我认为这可能是实现此类功能的唯一方法,因为我认为没有内置的解决方案。
I think this might be the only way of implementing such functionality, as I don't think there is a built in solution.
不要依赖其他方法。确保他们已经跑了。
Do not depend on other methods. Make sure they've ran.
以下方法只会在所有处理完成后才会抱怨 - 但是,如果这对您公平的话,它肯定会确保 foo() 已在父类中调用,否则会触发您可以执行的条件采取行动。
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.