Skip to content

[PropertyAccess] Allow to disable magic __get & __set #14125

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

Merged
Merged
Show file tree
Hide file tree
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
34 changes: 27 additions & 7 deletions components/property_access.rst
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ method::
$value = $propertyAccessor->getValue($person, 'birthday');


.. _components-property-access-magic-get:

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

Expand All @@ -213,6 +215,11 @@ The ``getValue()`` method can also use the magic ``__get()`` method::

var_dump($propertyAccessor->getValue($person, 'Wouter')); // [...]

.. versionadded:: 5.2

The magic `__get` method can be disabled since in Symfony 5.2.
see `Enable other Features`_.

.. _components-property-access-magic-call:

Magic ``__call()`` Method
Expand Down Expand Up @@ -273,6 +280,8 @@ also write to an array. This can be achieved using the
// or
// var_dump($person['first_name']); // 'Wouter'

.. _components-property-access-writing-to-objects:

Writing to Objects
------------------

Expand Down Expand Up @@ -351,6 +360,11 @@ see `Enable other Features`_::

var_dump($person->getWouter()); // [...]

.. versionadded:: 5.2

The magic `__set` method can be disabled since in Symfony 5.2.
see `Enable other Features`_.

Writing to Array Properties
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Expand Down Expand Up @@ -461,14 +475,20 @@ configured to enable extra features. To do that you could use the
// ...
$propertyAccessorBuilder = PropertyAccess::createPropertyAccessorBuilder();

// enables magic __call
$propertyAccessorBuilder->enableMagicCall();
$propertyAccessorBuilder->enableMagicCall(); // enables magic __call
$propertyAccessorBuilder->enableMagicGet(); // enables magic __get
$propertyAccessorBuilder->enableMagicSet(); // enables magic __set
$propertyAccessorBuilder->enableMagicMethods(); // enables magic __get, __set and __call

// disables magic __call
$propertyAccessorBuilder->disableMagicCall();
$propertyAccessorBuilder->disableMagicCall(); // enables magic __call
$propertyAccessorBuilder->disableMagicGet(); // enables magic __get
$propertyAccessorBuilder->disableMagicSet(); // enables magic __set
$propertyAccessorBuilder->disableMagicMethods(); // enables magic __get, __set and __call

// checks if magic __call handling is enabled
// checks if magic __call, __get or __set handling are enabled
$propertyAccessorBuilder->isMagicCallEnabled(); // true or false
$propertyAccessorBuilder->isMagicGetEnabled(); // true or false
$propertyAccessorBuilder->isMagicSetEnabled(); // true or false

// At the end get the configured property accessor
$propertyAccessor = $propertyAccessorBuilder->getPropertyAccessor();
Expand All @@ -480,7 +500,7 @@ configured to enable extra features. To do that you could use the

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

// ...
$propertyAccessor = new PropertyAccessor(true); // this enables handling of magic __call
// enable handling of magic __call, __set but not __get:
$propertyAccessor = new PropertyAccessor(PropertyAccessor::MAGIC_CALL | PropertyAccessor::MAGIC_SET);

.. _The Inflector component: https://github.com/symfony/inflector
28 changes: 28 additions & 0 deletions reference/configuration/framework.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ Configuration
* `property_access`_

* `magic_call`_
* `magic_get`_
* `magic_set`_
* `throw_exception_on_invalid_index`_
* `throw_exception_on_invalid_property_path`_

Expand Down Expand Up @@ -2127,6 +2129,32 @@ When enabled, the ``property_accessor`` service uses PHP's
:ref:`magic __call() method <components-property-access-magic-call>` when
its ``getValue()`` method is called.

magic_get
.........

**type**: ``boolean`` **default**: ``true``

When enabled, the ``property_accessor`` service uses PHP's
:ref:`magic __get() method <components-property-access-magic-get>` when
its ``getValue()`` method is called.

.. versionadded:: 5.2

The magic `magic_get` option was introduced in Symfony 5.2.

magic_set
.........

**type**: ``boolean`` **default**: ``true``

When enabled, the ``property_accessor`` service uses PHP's
:ref:`magic __set() method <components-property-access-writing-to-objects>` when
its ``setValue()`` method is called.

.. versionadded:: 5.2

The magic `magic_set` option was introduced in Symfony 5.2.

throw_exception_on_invalid_index
................................

Expand Down