0

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.

andho
  • 1,166
  • 1
  • 15
  • 27

1 Answers1

1

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.

GWW
  • 43,129
  • 11
  • 115
  • 108
  • 5
    Overloading *is* the concept of introducing a different set of parameters for the same function. You mean over*riding*. – GolezTrol Feb 21 '11 at 06:36
  • @GolezTrol: PHP does not fully support overloading. In an programming language where overloading was available, the code for andho would be perfectly legal. If ChildA::setB was called with ClassB argument, it would invoke SuperClass::setB, because ChildA does not have a setB which accepts ClassB. Overloading allows you to have multiple methods with the same name but with different arguments. The simplest way to walk around this in PHP is to not use type hinting. – jmz Feb 21 '11 at 06:49
  • I know. My comment was just to clarify the general difference to GWW, not to provide a full answer about the PHP implementation. If I were to do that, I'd have done that in a separate answer of my own. ;) – GolezTrol Feb 21 '11 at 13:29