- Create new test file for trait method writing injected private property on child object - Add trait PrivatePropertyWriter with private array property and items method - Implement PropertyOwner class using the trait - Create ChildPropertyOwner class extending PropertyOwner - Add main function to test the inherited private property access - Define expected output for the array dump resultpull/17/head
parent
9d3ea98199
commit
0ad6285cfe
1 changed files with 44 additions and 0 deletions
@ -0,0 +1,44 @@ |
||||
--TEST-- |
||||
trait method writes injected private property on child object |
||||
--SKIPIF-- |
||||
--FILE-- |
||||
<?php |
||||
|
||||
trait PrivatePropertyWriter |
||||
{ |
||||
private ?array $items = null; |
||||
|
||||
public function items(): array |
||||
{ |
||||
if ($this->items !== null) { |
||||
return $this->items; |
||||
} |
||||
return $this->items = [1, 3, 4]; |
||||
} |
||||
} |
||||
|
||||
class PropertyOwner |
||||
{ |
||||
use PrivatePropertyWriter; |
||||
} |
||||
|
||||
class ChildPropertyOwner extends PropertyOwner |
||||
{ |
||||
} |
||||
|
||||
function main(): void |
||||
{ |
||||
$object = new ChildPropertyOwner(); |
||||
var_dump($object->items()); |
||||
} |
||||
|
||||
?> |
||||
--EXPECT-- |
||||
array(3) { |
||||
[0]=> |
||||
int(1) |
||||
[1]=> |
||||
int(3) |
||||
[2]=> |
||||
int(4) |
||||
} |
||||
Loading…
Reference in new issue