Last active
January 17, 2018 10:34
-
-
Save hack3p/78eeca8f50d3297bf25768a9c72cafeb to your computer and use it in GitHub Desktop.
PHP invariant types. Test script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// example 1: return type | |
class A | |
{ | |
function foo(): A | |
{ | |
return $this; | |
} | |
} | |
class B extends A | |
{ | |
function foo(): B | |
{ | |
return $this; | |
} | |
} | |
// error: PHP Fatal error: Declaration of B::foo(): B must be compatible with A::foo(): A in ... | |
// example 2: arguments type hinting | |
class X {} | |
class Y extends X {} | |
class A | |
{ | |
function foo(X $bar) {} | |
} | |
class B extends A | |
{ | |
function foo(Y $bar) {} | |
} | |
// error: PHP Warning: Declaration of B::foo(Y $bar) should be compatible with A::foo(X $bar) in ... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@IMSoP nice graphics :)
With LSP in mind of course you're right.