/
php-82-dynamic-properties-allow.php
79 lines (67 loc) · 1.13 KB
/
php-82-dynamic-properties-allow.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<?php // lint >= 7.4
namespace Php82DynamicPropertiesAllow;
use AllowDynamicProperties;
/**
* @template T of object
*
* @property array<T> $properties
*/
trait TraitA {
/**
* @var array<T>
*/
public array $items = [];
}
/**
* @phpstan-use TraitA<ClassB>
*/
#[AllowDynamicProperties]
class ClassA {
/**
* @phpstan-use TraitA<ClassB>
*/
use TraitA;
}
class ClassB {
public function test(): void {
// empty
}
}
function (): void {
foreach ((new ClassA())->properties as $property) {
$property->test();
}
foreach ((new ClassA())->items as $item) {
$item->test();
}
};
#[AllowDynamicProperties]
class HelloWorld
{
public function __get(string $attribute): mixed
{
if($attribute == "world")
{
return "Hello World";
}
throw new \Exception("Attribute '{$attribute}' is invalid");
}
public function __isset(string $attribute)
{
try {
if (!isset($this->{$attribute})) {
$x = $this->{$attribute};
}
return isset($this->{$attribute});
} catch (\Exception $e) {
return false;
}
}
}
function (): void {
$hello = new HelloWorld();
if(isset($hello->world))
{
echo $hello->world;
}
};