3
class Param() {
}
class Subparam extends Param {
}

class Base {

  function mymethod(Param a) {
  }

}

class Sub extends Base {

 function mymethod(Subparam a) {
 }

}

In PHP, this leads to a warning: Declaration should be compatible with Base->mymethod(a : \Param)

What can I do to prevent that, other than using annotations only?

Lokomotywa
  • 2,624
  • 8
  • 44
  • 73

2 Answers2

-1

You can utilize interface:

interface Test {
}
class Param implements Test  {
}
class Subparam extends Param implements Test {
}

class Base {

  function mymethod(Test $a) {
  }

}

class Sub extends Base {

 function mymethod(Test $a) {
 }

}
za-ek
  • 467
  • 5
  • 11
-1
class Param() {
}
class Subparam extends Param {
}

class Base {

  function mymethod(a) {
    // Remove the parameter type from the method declaration.
    if (a !instanceof Param) {
      throw new \Exception('Paramater type should be of 'Param.');
    }

}

class Sub extends Base {

 function mymethod(a) {
 }

}

This should get you close.

David J Eddy
  • 1,999
  • 1
  • 19
  • 37