views:

43

answers:

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?

+4  A: 

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.

Anzeo
Thanks for your input. We've amended the abstract construct function so that it can accept a null id. If a null $id is found, we determine the calling class using get_called_class which we can then assign '_Null' to in order to instantiate the null object.