-
Notifications
You must be signed in to change notification settings - Fork 0
/
showcase.php
executable file
·129 lines (111 loc) · 3.64 KB
/
showcase.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
// check we're using at least PHP 8.2
if (version_compare(PHP_VERSION, '8.2.0', '<')) {
echo "\033[01;31mPHP 8.2 or higher is required. You are using " . PHP_VERSION . "\033[0m" . PHP_EOL;
exit(1);
}
// set the error reporting to ALL
error_reporting(E_ALL);
/**
* A simple Trait to set dynamic properties into a Class using this trait
*/
trait SetRandProps
{
/**
* Sets dynamic props into the class
*
* @param integer $min
* @param integer $max
*
* @return array
*/
protected function setRandProps() : array
{
// generate a randomic payload
$payload = ['_a' => 1, '_b' => 'string', '_c' => 1.1, '_d' => true, '_e' => ['a' => 1, 'b' => 2], '_f' => new stdClass()];
// for each key in the payload, create a dynamic property
foreach ($payload as $key => $value) {
$this->{$key} = $value;
}
return $payload;
}
}
/**
* This class is not using the #[AllowDynamicProperties] attribute, so it will trigger a deprecation warning.
* Dynamic properties are set within the constructor
*/
class Deprecated
{
use SetRandProps;
public function __construct()
{
$this->setRandProps();
}
}
/**
* This class is using the #[AllowDynamicProperties] attribute, so it will not trigger a deprecation warning.
* Dynamic properties are set within the constructor
*/
#[AllowDynamicProperties]
class DynPropsOnConstructor
{
use SetRandProps;
public function __construct()
{
$this->setRandProps();
}
}
/**
* This class extends the DynPropsOnConstructor class, so it will not trigger a deprecation warning.
* Dynamic properties are set within the constructor
*/
class DynPropsOnConstructorExtended extends DynPropsOnConstructor
{
public function __construct()
{
parent::__construct();
}
}
/**
* This class is using the #[AllowDynamicProperties] attribute, so it will not trigger a deprecation warning.
* Dynamic properties are set by explicitly calling the setProps() method
*/
#[AllowDynamicProperties]
class DynPropsByExplicitCall
{
use SetRandProps;
public ?string $publicProp = null;
public function setProps()
{
$this->setRandProps();
}
}
/**
* This class extends the DynPropsByExplicitCall class, so it will not trigger a deprecation warning.
* Dynamic properties are set by explicitly calling the setProps() method
*/
class DynPropsByExplicitCallExtended extends DynPropsByExplicitCall
{
public function setProps()
{
parent::setProps();
$this->publicProp = 'overridden';
}
}
// test the deprecated behavior
echo "\033[01;31mDeprecated behavior (will trigger deprecation warnings)\033[0m" . PHP_EOL;
$deprecated = new Deprecated();
// test the new behavior
echo "\033[01;32mNew behavior (will not trigger deprecation warnings)\033[0m" . PHP_EOL;
$dynPropsOnConstructor = new DynPropsOnConstructor();
// test the new behavior, with inheritance
echo "\033[01;32mNew behavior, with inheritance (will not trigger deprecation warnings)\033[0m" . PHP_EOL;
$dynPropsOnConstructorExtended = new DynPropsOnConstructorExtended();
// test the new behavior, with explicit call
echo "\033[01;32mNew behavior, with explicit call (will not trigger deprecation warnings)\033[0m" . PHP_EOL;
$dynPropsByExplicitCall = new DynPropsByExplicitCall();
$dynPropsByExplicitCall->setProps();
// test the new behavior, with explicit call and inheritance
echo "\033[01;32mNew behavior, with explicit call and inheritance (will not trigger deprecation warnings)\033[0m" . PHP_EOL;
$dynPropsByExplicitCallExtended = new DynPropsByExplicitCallExtended();
$dynPropsByExplicitCallExtended->setProps();