Parameter Type Widening に関して
<?php | |
class ParentClassA { function f(stdClass $a) {} } | |
class ChildClassA extends ParentClassA { function f($a) {} } | |
/* | |
7.2 の Parameter Type Widening (https://wiki.php.net/rfc/parameter-no-type-variance) によって、 | |
上記 ParentClassA/ChildClassA は ok になった。 | |
(new ParentClassA())->f(new stdClass()) に対して | |
(new ChildClassA())->f(new stdClass()) と(派生型への)置き換えができるので、リスコフの置換原則に反しないとのこと。 | |
ちなみに、 7.1 までは `PHP Warning: Declaration of ChildClassA::f($a) should be compatible with ParentClassA::f(stdClass $a)` | |
派生クラスのメソッド引数で反変性が認められるなら、B から派生した D をベースクラスの引数として、派生クラスで B にしてもOK かと思ったけど | |
下のようなコードは Warning になってしまうのでした。Umm... | |
(これダメなので、当然ながら interface や abstruct Class もだめ) | |
*/ | |
class B {}; | |
class D extends B {} | |
class ParentClassB { function f(D $a) {} } | |
class ChildClassB extends ParentClassB { function f(B $a) {} } | |
// PHP Warning: Declaration of ChildClassB::f(B $a) should be compatible with ParentClassB::f(D $a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment