2

Consider the following two classes:

class A {
   ...

   testMethod($param = null) {
       ...
   }
}

class B extends A {
   ...

   testMethod($additionalParam, $param = null) {
      ... do something with $additionalParam ...
      parent::testMethod($param);
   }
}

intelephense is raising me an error, that the two methods are not compatible (they are indeed not). My script although runs fine, nonetheless.

Could someone explain to me, why would intelephense be bothered by this design? Wouldn't this design be considered valid? If not, if I would like to extend a method in a sub class, by storing additional information, which I didn't store in the base class, how should I proceed?

Adam Baranyai
  • 3,635
  • 3
  • 29
  • 68
  • 4
    I think [Why is overriding method parameters a violation of strict standards in PHP?](https://stackoverflow.com/questions/13423494/why-is-overriding-method-parameters-a-violation-of-strict-standards-in-php) explains a lot about why this is a problem. There are also links in there to other bits. – Nigel Ren Jul 18 '20 at 07:43

1 Answers1

0

Cannot possible overwrite same method with diff parameters, for this try change the name testMethod to testMethodMain or another name in your class B.

Darlan Dieterich
  • 2,369
  • 1
  • 27
  • 37