扩展类的问题
我在扩展一个类时遇到问题,该类本身进一步扩展了一个抽象类。
基本抽象类具有以下方法:
摘要:
private final __construct()
abstract protected function loadFromId()
private static final load($id)
类 1 扩展 抽象:
protected loadFromId()
类 2 扩展类 1:
//nothing as of yet
我从类 2 扩展类 1 的原因是因为我需要它返回类 1 的实例。类 2 基本上将返回一个空对象以进行验证。
如果我尝试扩展 Class 1:
Class 2 extends Class 1 { }
我收到以下错误“Cannot override Final method class::__construct()
”,显然是因为它是私有方法。
有没有办法可以基于 Class 1 创建空对象?
I'm having a problem extending a class which itself further extends a abstract class.
The base abstract class has the following methods:
Abstract:
private final __construct()
abstract protected function loadFromId()
private static final load($id)
Class 1 extends Abstract:
protected loadFromId()
Class 2 extends Class 1:
//nothing as of yet
The reason I'm extending Class 1 from Class 2 is because I need it to return an instance of Class 1. Class 2 will basically return a null object for validation purposes.
If I attempt to extend Class 1:
Class 2 extends Class 1 { }
I receive the following error "Cannot override final method class::__construct()
", obviously because it is a private method.
Is there a way I can create a null object based on Class 1?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您收到的错误是由于您将 Abstract 超类中的 construct() 函数声明为 Final 导致的,这意味着您无法覆盖它。把它去掉应该就没有问题了。
旁注:使用继承时使用 protected 更安全。除非您绝对确定您的子类中不需要该字段/函数。
The error you're receiving is caused by the fact you declared your construct() function in the Abstract super class as final, meaning you cannot override it. Remove it and there shouldn't be a problem.
On a sidenote: it's safer to use protected when using inheritance. Unless you are absolutely sure you won't be needing the field/function in your child classes.