Skip to content

Commit a92ae45

Browse files
committed
:octocat:
1 parent 21838f4 commit a92ae45

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

README.md

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,10 @@ var_dump($container->nope); // -> null
8585
```
8686

8787
### Advanced usage
88+
89+
Suppose the following trait from library 1:
90+
8891
```php
89-
// from library 1
9092
trait SomeOptions{
9193
protected string $foo;
9294
protected string $what;
@@ -99,7 +101,7 @@ trait SomeOptions{
99101
}
100102

101103
/*
102-
* special prefixed magic setters & getters
104+
* special prefixed magic setters & getters ("set_"/"get_" + property name)
103105
*/
104106

105107
// this method will be called from __set() when property $what is set
@@ -112,13 +114,18 @@ trait SomeOptions{
112114
return 'hash: '.$this->what;
113115
}
114116
}
117+
```
115118

116-
// from library 2
119+
And another trait from library 2:
120+
121+
```php
117122
trait MoreOptions{
118123
protected string $bar = 'whatever'; // provide default values
119124
}
120125
```
121126

127+
We can now plug the several library options together to a single class/object:
128+
122129
```php
123130
$commonOptions = [
124131
// SomeOptions
@@ -127,7 +134,6 @@ $commonOptions = [
127134
'bar' => 'nothing',
128135
];
129136

130-
// now plug the several library options together to a single object
131137
$container = new class ($commonOptions) extends SettingsContainerAbstract{
132138
use SomeOptions, MoreOptions;
133139
};
@@ -139,7 +145,7 @@ $container->what = 'some value';
139145
var_dump($container->what); // -> hash: 5946210c9e93ae37891dfe96c3e39614 (custom getter added "hash: ")
140146
```
141147

142-
#### A note on property hooks (PHP 8.4+)
148+
### A note on property hooks (PHP 8.4+)
143149

144150
Property hooks are called whenever a property is accessed (except from within the hook itself of course), which means that the custom get/set methods this library allows would conflict when a custom method is defined for a property that also has a hook defined.
145151
To prevent double method calls, the internal methods `hasSetHook()` and `hasGetHook()` have been introduced, and are called whenever the magic get/set methods are called: when both, a custom method and a property hook exist, only the property hook will be called.
@@ -206,5 +212,5 @@ class PropertyHooksContainer extends SettingsContainerAbstract{
206212

207213

208214
## Disclaimer
209-
This might be either an absolutely brilliant or completely stupid idea - you decide. (in hindsight it was a great idea I guess - property hooks made their way into PHP 8.4)
210-
Also, this is not a dependency injection container. Stop using DI containers FFS.
215+
This might be either an absolutely brilliant or completely stupid idea - you decide (in hindsight it was a great idea I guess - property hooks made their way into PHP 8.4).
216+
<br/>Also, this is not a dependency injection container. Stop using DI containers FFS.

0 commit comments

Comments
 (0)