7

I want to clarify a problem i am having

I have a base DataBase class that will be inherited by a bunch of other classes.The constructor looks like this:

public function __construct ($table)
{
     $this->table = $table;
     $this->db = new Database();
     $this->db->connect();
}

I will call from this constructor from children as following:

 public function __construct ($something)
{
    parent::__construct("planets_games");
}

My problem is that php doesn't allow me to make the child's constructor without the $something parameter i get the following:

 Fatal error: Declaration of planetsGames::__construct() must be compatible with that of IScaffold::__construct()

I am currently bypassing this by instantiating an object like this:

$pg = new planetsGames('uselessStringHereThatHasNoUtilityAtAll');

I think i am missing something very important in my basic php knowledge

Thank you very much for the help in advance

1
  • Yes php currently is strict that child class's construct parameter must be compatible to that of parent. And what is it that you are not getting? You cannot do something like that but yes you can have no arguments to both parent and child; yet pass arguments to parent only from child's construct. Is this something you wish for ? Commented Mar 17, 2013 at 10:50

2 Answers 2

5

This error message refers to the liskov substitution principle. It applies to every IS-A relationship (which is the meaning of using inheritance (extends)) and states that every subtype should be fully replacable for the super type.

But this doesn´t apply to constructors! Which php version you are using?

It seems the base class has marked the constructor as abstract. That´s the only way this error can appear.

You should never mark constructors abstract, final or put them in interfaces!

In most languages this isn´t even possible.

What you should take away from this is that the best-practice is that each concrete object has a constructor with a signature that best represents how a consumer should fully instantiate that particular object. In some cases where inheritance is involved, “borrowing” the parents constructor is acceptable and useful. Furthermore, it is encouraged that when you subclass a particular type, that your new type should, when appropriate, have its own constructor that makes the most sense to the new subtype.

http://ralphschindler.com/2012/03/09/php-constructor-best-practices-and-the-prototype-pattern

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

3 Comments

Sorry for the late reply.I am using version 5.3.13
Isn't LSP suitable only when we talk about polymorphism? My initial thought was the same, but it's a constructor, not a regular method. We don't have contracts for constructors, do we?
No, but in php you can mark constructors abstract. I think in a super class (IScaffold?) in the above example this is the case, otherwise the fatal wouldn´t occur. Making the constructor abstract brings in the same constraints from LSP. I think this is bad practice, as i stated above.
1

A few years late to the party....

The problem is your constructor is needing a value. You can prevent the fatal error by setting a default value such as an empty string.

public function __construct($something = "")
{
    parent::__construct("planets_games");
}

Then just instantiate the class like normal

$pg = new planetsGames();

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.