parent
9d22e8ca24
commit
78039a28db
7 changed files with 299 additions and 0 deletions
@ -0,0 +1,32 @@ |
|||||||
|
--TEST-- |
||||||
|
Trait method with `parent` return type flattened into a subclass |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
|
||||||
|
class Base |
||||||
|
{ |
||||||
|
} |
||||||
|
|
||||||
|
trait TestTrait |
||||||
|
{ |
||||||
|
public function who(): parent |
||||||
|
{ |
||||||
|
return $this; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
class Child extends Base |
||||||
|
{ |
||||||
|
use TestTrait; |
||||||
|
} |
||||||
|
|
||||||
|
function main() |
||||||
|
{ |
||||||
|
$c = new Child; |
||||||
|
// The trait method's `parent` resolves to the consuming class's parent (Base). |
||||||
|
$r = $c->who(); |
||||||
|
var_dump($r instanceof Child); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
bool(true) |
||||||
@ -0,0 +1,38 @@ |
|||||||
|
--TEST-- |
||||||
|
Trait method with `self` return type flattened into a class that implements an interface declaring `self` return |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
|
||||||
|
interface TestInterface |
||||||
|
{ |
||||||
|
public function test(): self; |
||||||
|
} |
||||||
|
|
||||||
|
trait TestTrait |
||||||
|
{ |
||||||
|
public function test(): self |
||||||
|
{ |
||||||
|
return $this; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
class TestClass implements TestInterface |
||||||
|
{ |
||||||
|
use TestTrait; |
||||||
|
} |
||||||
|
|
||||||
|
function main() |
||||||
|
{ |
||||||
|
$test = new TestClass; |
||||||
|
// The trait method's `self` resolves to the consuming class (TestClass), |
||||||
|
// which must be compatible with the interface's `self` (TestInterface). |
||||||
|
$result = $test->test(); |
||||||
|
var_dump($result instanceof TestClass); |
||||||
|
var_dump($result === $test); |
||||||
|
var_dump($result instanceof TestInterface); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
bool(true) |
||||||
|
bool(true) |
||||||
|
bool(true) |
||||||
@ -0,0 +1,35 @@ |
|||||||
|
--TEST-- |
||||||
|
Trait method with `static` return type flattened into a class that implements an interface declaring `static` return |
||||||
|
--FILE-- |
||||||
|
<?php |
||||||
|
|
||||||
|
interface TestInterface |
||||||
|
{ |
||||||
|
public function make(): static; |
||||||
|
} |
||||||
|
|
||||||
|
trait TestTrait |
||||||
|
{ |
||||||
|
public function make(): static |
||||||
|
{ |
||||||
|
return new static; |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
class TestClass implements TestInterface |
||||||
|
{ |
||||||
|
use TestTrait; |
||||||
|
} |
||||||
|
|
||||||
|
function main() |
||||||
|
{ |
||||||
|
$a = new TestClass; |
||||||
|
$b = $a->make(); |
||||||
|
// `static` is late-static-bound to the consuming class (TestClass). |
||||||
|
var_dump($b instanceof TestClass); |
||||||
|
var_dump($b !== $a); |
||||||
|
} |
||||||
|
?> |
||||||
|
--EXPECT-- |
||||||
|
bool(true) |
||||||
|
bool(true) |
||||||
Loading…
Reference in new issue