@@ -157,10 +157,10 @@ getters, this means that you can do something like this::
157
157
158
158
This will produce: ``He is an author ``
159
159
160
- Magic Methods
161
- ~~~~~~~~~~~~~
160
+ Magic `` __get() `` Method
161
+ ~~~~~~~~~~~~~~~~~~~~~~~~
162
162
163
- At last, ``getValue `` can use the magic ``__get `` method too ::
163
+ The ``getValue `` method can also use the magic ``__get `` method::
164
164
165
165
// ...
166
166
class Person
@@ -179,6 +179,49 @@ At last, ``getValue`` can use the magic ``__get`` method too::
179
179
180
180
echo $accessor->getValue($person, 'Wouter'); // array(...)
181
181
182
+ Magic ``__call() `` Method
183
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
184
+
185
+ At last, ``getValue `` can use the magic ``__call `` method, but you need to
186
+ enable this feature by using :class: `Symfony\\ Component\\ PropertyAccess\\ PropertyAccessorBuilder `::
187
+
188
+ // ...
189
+ class Person
190
+ {
191
+ private $children = array(
192
+ 'wouter' => array(...),
193
+ );
194
+
195
+ public function __call($name, $args)
196
+ {
197
+ $property = lcfirst(substr($name, 3));
198
+ if ('get' === substr($name, 0, 3)) {
199
+ return isset($this->children[$property]) ? $this->children[$property] : null;
200
+ } elseif ('set' === substr($name, 0, 3)) {
201
+ $value = 1 == count($args) ? $args[0] : null;
202
+ $this->children[$property] = $value;
203
+ }
204
+ }
205
+ }
206
+
207
+ $person = new Person();
208
+
209
+ // Enable magic __call
210
+ $accessor = PropertyAccess::getPropertyAccessorBuilder()
211
+ ->enableMagicCall()
212
+ ->getPropertyAccessor();
213
+
214
+ echo $accessor->getValue($person, 'wouter'); // array(...)
215
+
216
+ .. versionadded :: 2.3
217
+ The use of magic ``__call() `` method was added in Symfony 2.3.
218
+
219
+ .. caution ::
220
+
221
+ The ``__call `` feature is disabled by default, you can enable it by calling
222
+ :method: `PropertyAccessorBuilder::enableMagicCallEnabled<Symfony\\ Component\\ PropertyAccess\\ PropertyAccessorBuilder::enableMagicCallEnabled> `
223
+ see `Enable other Features `_.
224
+
182
225
Writing to Arrays
183
226
-----------------
184
227
@@ -223,7 +266,7 @@ can use setters, the magic ``__set`` or properties to set values::
223
266
}
224
267
225
268
$person = new Person();
226
-
269
+
227
270
$accessor->setValue($person, 'firstName', 'Wouter');
228
271
$accessor->setValue($person, 'lastName', 'de Jong');
229
272
$accessor->setValue($person, 'children', array(new Person()));
@@ -232,6 +275,38 @@ can use setters, the magic ``__set`` or properties to set values::
232
275
echo $person->getLastName(); // 'de Jong'
233
276
echo $person->children; // array(Person());
234
277
278
+ You can also use ``__call `` to set values but you need to enable the feature,
279
+ see `Enable other Features `_.
280
+
281
+ // ...
282
+ class Person
283
+ {
284
+ private $children = array();
285
+
286
+ public function __call($name, $args)
287
+ {
288
+ $property = lcfirst(substr($name, 3));
289
+ if ('get' === substr($name, 0, 3)) {
290
+ return isset($this->children[$property]) ? $this->children[$property] : null;
291
+ } elseif ('set' === substr($name, 0, 3)) {
292
+ $value = 1 == count($args) ? $args[0] : null;
293
+ $this->children[$property] = $value;
294
+ }
295
+ }
296
+
297
+ }
298
+
299
+ $person = new Person();
300
+
301
+ // Enable magic __call
302
+ $accessor = PropertyAccess::getPropertyAccessorBuilder()
303
+ ->enableMagicCall()
304
+ ->getPropertyAccessor();
305
+
306
+ $accessor->setValue($person, 'wouter', array(...));
307
+
308
+ echo $person->getWouter() // array(...)
309
+
235
310
Mixing Objects and Arrays
236
311
-------------------------
237
312
@@ -265,4 +340,37 @@ You can also mix objects and arrays::
265
340
echo 'Hello '.$accessor->getValue($person, 'children[0].firstName'); // 'Wouter'
266
341
// equal to $person->getChildren()[0]->firstName
267
342
343
+ Enable other Features
344
+ ~~~~~~~~~~~~~~~~~~~~~
345
+
346
+ The :class: `Symfony\\ Component\\ PropertyAccess\\ PropertyAccessor ` can be
347
+ configured to enable extra features. To do that you could use the
348
+ :class: `Symfony\\ Component\\ PropertyAccess\\ PropertyAccessorBuilder `::
349
+
350
+ // ...
351
+ $accessorBuilder = PropertyAccess::getPropertyAccessorBuilder();
352
+
353
+ // Enable magic __call
354
+ $accessorBuilder->enableMagicCall();
355
+
356
+ // Disable magic __call
357
+ $accessorBuilder->disableMagicCall();
358
+
359
+ // Check if magic __call handling is enabled
360
+ $accessorBuilder->isMagicCallEnabled() // true or false
361
+
362
+ // At the end get the configured property accessor
363
+ $accessor = $accessorBuilder->getPropertyAccessor();
364
+
365
+ // Or all in one
366
+ $accessor = PropertyAccess::getPropertyAccessorBuilder()
367
+ ->enableMagicCall()
368
+ ->getPropertyAccessor();
369
+
370
+ Or you can pass parameters directly to the constructor (not the recommended way)::
371
+
372
+ // ...
373
+ $accessor = new PropertyAccessor(true) // this enable handling of magic __call
374
+
375
+
268
376
.. _Packagist : https://packagist.org/packages/symfony/property-access
0 commit comments