我有一个关于在 PHP 中扩展类的问题。
我在php网站上看到的例子,方法里只有1行代码……如果方法里有很多代码,是不是一样??
如果这是基类:
class BaseClass {
public function WithWayTooMuchCode {
// like 100 lines of code here
}
}
如果我想使用相同的方法,但只更改一两处,是否必须复制所有代码?
class MyOwnClass extends BaseClass {
public function WithWayTooMuchCode {
// like 100 lines of code here
// do I have to copy all of the other code and then add my code??
}
}
这对我来说似乎有点不干......
最佳答案
是的,除非那 1 或 2 件事恰好在开头或结尾。您可以通过
调用父函数parent::WithWayTooMuchCode();
您可以将其放置在子方法/重写方法中的任何位置。
如果感觉不干,请考虑将函数拆分为更小的方法。
关于PHP 扩展一个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10554135/