class Param() {
}
class Subparam extends Param {
}
class Base {
function mymethod(Param a) {
}
}
class Sub extends Base {
function mymethod(Subparam a) {
}
}
在PHP中,这会导致警告:Declaration should be compatible with Base->mymethod(a : \Param)
除了仅使用注释外,我该怎么做才能防止这种情况?
答案 0 :(得分:-1)
您可以使用界面:
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) {
}
}
答案 1 :(得分:-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) {
}
}
这应该让你接近。