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?

1
  • 1
    You should only get Cannot override final method class::__construct() if you actually placed another constructor in one of the subclasses (which obviously cannot work because of the final keyword). Otherwise, you should get a Fatal error: Call to private A::__construct() from invalid context. Commented Oct 29, 2010 at 9:09

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.