andho
andho

Reputation: 1172

PHP method overloading

I have been developing an ORM and currently run into this error in testing.

Declaration of ClassA::setB() should be compatible with that of SuperClass::setB()

My code is something like this.

class SuperClass() {

  protected $b;

  public function setB(ClassB $b) {
    $this->b = $b;
  }

}

class ClassB {}

class ChildB extends ClassB {}

class ClassA extends SuperClass {

  public function setB(ChildB $b) {
    parent::setB($b);
  }

}

Is this excepted behaviour. What I think is ClassA is overloading method setB of SuperClass, but ClassA::setB is still compatible with SuperClass::setB because class ChildB is a child class of ClassB.

Or is overloading in anyway just prohibited.

Upvotes: 0

Views: 1460

Answers (1)

GWW
GWW

Reputation: 44161

In PHP the parameters of an overloaded method should be identical. You can hide the error by turning of STRICT error reporting, otherwise you need to make the parameters identical. However, it's probably in your best interest to fix the parameters to maintain forward compatibility with new PHP versions.

Upvotes: 1

Related Questions