Skip to content

Instantly share code, notes, and snippets.

@brzuchal
Created November 9, 2016 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brzuchal/8add0fc826410b5cb7c3ed76b9a67fbd to your computer and use it in GitHub Desktop.
Save brzuchal/8add0fc826410b5cb7c3ed76b9a67fbd to your computer and use it in GitHub Desktop.
Object typehint & return type variance
<?php
class A {}
class B {
function foo(object $a) : object {
return $a;
}
}
class C extends B {
function foo(A $a) : A {
return $a;
}
} // Fatal error: Declaration of C::foo(A $a): A must be compatible with B::foo(object $a): object in object-variance.php on line 14
$a = new A();
$b = new B();
$b->foo($a);
$c = new C();
$c->foo($a);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment