Skip to content

Add documentation for the use of __call with propertyAccess #2472

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
116 changes: 112 additions & 4 deletions components/property_access/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,10 @@ getters, this means that you can do something like this::

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

Magic Methods
~~~~~~~~~~~~~
Magic ``__get()`` Method
~~~~~~~~~~~~~~~~~~~~~~~~

At last, ``getValue`` can use the magic ``__get`` method too::
The ``getValue`` method can also use the magic ``__get`` method::

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

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

Magic ``__call()`` Method
~~~~~~~~~~~~~~~~~~~~~~~~~

At last, ``getValue`` can use the magic ``__call`` method, but you need to
enable this feature by using :class:`Symfony\\Component\\PropertyAccess\\PropertyAccessorBuilder`::

// ...
class Person
{
private $children = array(
'wouter' => array(...),
);

public function __call($name, $args)
{
$property = lcfirst(substr($name, 3));
if ('get' === substr($name, 0, 3)) {
return isset($this->children[$property]) ? $this->children[$property] : null;
} elseif ('set' === substr($name, 0, 3)) {
$value = 1 == count($args) ? $args[0] : null;
$this->children[$property] = $value;
}
}
}

$person = new Person();

// Enable magic __call
$accessor = PropertyAccess::getPropertyAccessorBuilder()
->enableMagicCall()
->getPropertyAccessor();

echo $accessor->getValue($person, 'wouter'); // array(...)

.. versionadded:: 2.3
The use of magic ``__call()`` method was added in Symfony 2.3.

.. caution::

The ``__call`` feature is disabled by default, you can enable it by calling
:method:`PropertyAccessorBuilder::enableMagicCallEnabled<Symfony\\Component\\PropertyAccess\\PropertyAccessorBuilder::enableMagicCallEnabled>`
see `Enable other Features`_.

Writing to Arrays
-----------------

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

$person = new Person();

$accessor->setValue($person, 'firstName', 'Wouter');
$accessor->setValue($person, 'lastName', 'de Jong');
$accessor->setValue($person, 'children', array(new Person()));
Expand All @@ -232,6 +275,38 @@ can use setters, the magic ``__set`` or properties to set values::
echo $person->getLastName(); // 'de Jong'
echo $person->children; // array(Person());

You can also use ``__call`` to set values but you need to enable the feature,
see `Enable other Features`_.

// ...
class Person
{
private $children = array();

public function __call($name, $args)
{
$property = lcfirst(substr($name, 3));
if ('get' === substr($name, 0, 3)) {
return isset($this->children[$property]) ? $this->children[$property] : null;
} elseif ('set' === substr($name, 0, 3)) {
$value = 1 == count($args) ? $args[0] : null;
$this->children[$property] = $value;
}
}

}

$person = new Person();

// Enable magic __call
$accessor = PropertyAccess::getPropertyAccessorBuilder()
->enableMagicCall()
->getPropertyAccessor();

$accessor->setValue($person, 'wouter', array(...));

echo $person->getWouter() // array(...)

Mixing Objects and Arrays
-------------------------

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

Enable other Features
~~~~~~~~~~~~~~~~~~~~~

The :class:`Symfony\\Component\\PropertyAccess\\PropertyAccessor` can be
configured to enable extra features. To do that you could use the
:class:`Symfony\\Component\\PropertyAccess\\PropertyAccessorBuilder`::

// ...
$accessorBuilder = PropertyAccess::getPropertyAccessorBuilder();

// Enable magic __call
$accessorBuilder->enableMagicCall();

// Disable magic __call
$accessorBuilder->disableMagicCall();

// Check if magic __call handling is enabled
$accessorBuilder->isMagicCallEnabled() // true or false

// At the end get the configured property accessor
$accessor = $accessorBuilder->getPropertyAccessor();

// Or all in one
$accessor = PropertyAccess::getPropertyAccessorBuilder()
->enableMagicCall()
->getPropertyAccessor();

Or you can pass parameters directly to the constructor (not the recommended way)::

// ...
$accessor = new PropertyAccessor(true) // this enable handling of magic __call


.. _Packagist: https://packagist.org/packages/symfony/property-access