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?
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.
精彩评论