Skip to content

Commit b140d5c

Browse files
jaugustinweaverryan
authored andcommitted
add documentation for the use of __call with propertyAccess
1 parent 59d6e6e commit b140d5c

File tree

1 file changed

+112
-4
lines changed

1 file changed

+112
-4
lines changed

components/property_access/introduction.rst

Lines changed: 112 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@ getters, this means that you can do something like this::
157157

158158
This will produce: ``He is an author``
159159

160-
Magic Methods
161-
~~~~~~~~~~~~~
160+
Magic ``__get()`` Method
161+
~~~~~~~~~~~~~~~~~~~~~~~~
162162

163-
At last, ``getValue`` can use the magic ``__get`` method too::
163+
The ``getValue`` method can also use the magic ``__get`` method::
164164

165165
// ...
166166
class Person
@@ -179,6 +179,49 @@ At last, ``getValue`` can use the magic ``__get`` method too::
179179

180180
echo $accessor->getValue($person, 'Wouter'); // array(...)
181181

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+
182225
Writing to Arrays
183226
-----------------
184227

@@ -223,7 +266,7 @@ can use setters, the magic ``__set`` or properties to set values::
223266
}
224267

225268
$person = new Person();
226-
269+
227270
$accessor->setValue($person, 'firstName', 'Wouter');
228271
$accessor->setValue($person, 'lastName', 'de Jong');
229272
$accessor->setValue($person, 'children', array(new Person()));
@@ -232,6 +275,38 @@ can use setters, the magic ``__set`` or properties to set values::
232275
echo $person->getLastName(); // 'de Jong'
233276
echo $person->children; // array(Person());
234277

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+
235310
Mixing Objects and Arrays
236311
-------------------------
237312

@@ -265,4 +340,37 @@ You can also mix objects and arrays::
265340
echo 'Hello '.$accessor->getValue($person, 'children[0].firstName'); // 'Wouter'
266341
// equal to $person->getChildren()[0]->firstName
267342

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+
268376
.. _Packagist: https://packagist.org/packages/symfony/property-access

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy