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.

1 Answer 1

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.

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

3 Comments

Overloading is the concept of introducing a different set of parameters for the same function. You mean overriding.
@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.
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. ;)

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.