Skip to content

Instantly share code, notes, and snippets.

@hack3p
Last active January 17, 2018 10:34
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hack3p/78eeca8f50d3297bf25768a9c72cafeb to your computer and use it in GitHub Desktop.
Save hack3p/78eeca8f50d3297bf25768a9c72cafeb to your computer and use it in GitHub Desktop.
PHP invariant types. Test script.
<?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 ...
@quebits
Copy link

quebits commented Jan 17, 2018

@IMSoP nice graphics :)

With LSP in mind of course you're right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment