4

I'd like to create an interface for entities that are CRUDable (can be saved and deleted). Here is my abstract class:

abstract class AbstractCrudableEntity extends AbstractEntity
{
    abstract public function doSave();
    abstract public function doDelete();
}

My implementing class needs a couple extra parameters to those methods. Here is the signature of the implementing class:

class Contact extends AbstractCrudableEntity {
    public function doSave(User $user, \UberClientManager $manager);
    public function doDelete(User $user, \UberClientManager $manager);
}

I understand that PHP requires that implementing classes have the same parameters for the methods as the parent class (there are several questions that answer this question: this, for example). So that is not the problem.

However, I recently came across some code in Symfony dealing with authentication tokens. The class UsernamePasswordToken extends AbstractToken, and has a different set of parameters in the __construct() method: AbstractToken::__construct() versus UsernamePasswordToken::__construct().

My question is how is Symfony able to do this? What is the difference between this and my code?

Community
  • 1
  • 1
theunraveler
  • 3,254
  • 20
  • 19

2 Answers2

8

Overriding constructors is a special case:

Unlike with other methods, PHP will not generate an E_STRICT level error message when __construct() is overridden with different parameters than the parent __construct() method has.

You can not do that with other methods.

Elnur Abdurrakhimov
  • 44,533
  • 10
  • 148
  • 133
0

Your child methods must have the same number of parameters as the abstract methods in an abstract parent class.

The constructor in you example is not abstract--the child is just overriding it.

Ray
  • 40,256
  • 21
  • 101
  • 138
  • This doesn't matter. In fact, I tried declaring the methods in `AbstractCrudableEntity` as not abstract, and still got the error. – theunraveler Oct 31 '12 at 12:51