Skip to content

Commit a8900ca

Browse files
committed
minor #14125 [PropertyAccess] Allow to disable magic __get & __set (ogizanagi)
This PR was merged into the master branch. Discussion ---------- [PropertyAccess] Allow to disable magic __get & __set Fixes #14107 Commits ------- 8631322 [PropertyAccess] Allow to disable magic __get & __set
2 parents 10f7213 + 8631322 commit a8900ca

File tree

2 files changed

+55
-7
lines changed

2 files changed

+55
-7
lines changed

components/property_access.rst

+27-7
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,8 @@ method::
191191
$value = $propertyAccessor->getValue($person, 'birthday');
192192

193193

194+
.. _components-property-access-magic-get:
195+
194196
Magic ``__get()`` Method
195197
~~~~~~~~~~~~~~~~~~~~~~~~
196198

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

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

218+
.. versionadded:: 5.2
219+
220+
The magic `__get` method can be disabled since in Symfony 5.2.
221+
see `Enable other Features`_.
222+
216223
.. _components-property-access-magic-call:
217224

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

283+
.. _components-property-access-writing-to-objects:
284+
276285
Writing to Objects
277286
------------------
278287

@@ -351,6 +360,11 @@ see `Enable other Features`_::
351360

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

363+
.. versionadded:: 5.2
364+
365+
The magic `__set` method can be disabled since in Symfony 5.2.
366+
see `Enable other Features`_.
367+
354368
Writing to Array Properties
355369
~~~~~~~~~~~~~~~~~~~~~~~~~~~
356370

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

464-
// enables magic __call
465-
$propertyAccessorBuilder->enableMagicCall();
478+
$propertyAccessorBuilder->enableMagicCall(); // enables magic __call
479+
$propertyAccessorBuilder->enableMagicGet(); // enables magic __get
480+
$propertyAccessorBuilder->enableMagicSet(); // enables magic __set
481+
$propertyAccessorBuilder->enableMagicMethods(); // enables magic __get, __set and __call
466482

467-
// disables magic __call
468-
$propertyAccessorBuilder->disableMagicCall();
483+
$propertyAccessorBuilder->disableMagicCall(); // enables magic __call
484+
$propertyAccessorBuilder->disableMagicGet(); // enables magic __get
485+
$propertyAccessorBuilder->disableMagicSet(); // enables magic __set
486+
$propertyAccessorBuilder->disableMagicMethods(); // enables magic __get, __set and __call
469487

470-
// checks if magic __call handling is enabled
488+
// checks if magic __call, __get or __set handling are enabled
471489
$propertyAccessorBuilder->isMagicCallEnabled(); // true or false
490+
$propertyAccessorBuilder->isMagicGetEnabled(); // true or false
491+
$propertyAccessorBuilder->isMagicSetEnabled(); // true or false
472492

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

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

483-
// ...
484-
$propertyAccessor = new PropertyAccessor(true); // this enables handling of magic __call
503+
// enable handling of magic __call, __set but not __get:
504+
$propertyAccessor = new PropertyAccessor(PropertyAccessor::MAGIC_CALL | PropertyAccessor::MAGIC_SET);
485505

486506
.. _The Inflector component: https://github.com/symfony/inflector

reference/configuration/framework.rst

+28
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,8 @@ Configuration
167167
* `property_access`_
168168

169169
* `magic_call`_
170+
* `magic_get`_
171+
* `magic_set`_
170172
* `throw_exception_on_invalid_index`_
171173
* `throw_exception_on_invalid_property_path`_
172174

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

2132+
magic_get
2133+
.........
2134+
2135+
**type**: ``boolean`` **default**: ``true``
2136+
2137+
When enabled, the ``property_accessor`` service uses PHP's
2138+
:ref:`magic __get() method <components-property-access-magic-get>` when
2139+
its ``getValue()`` method is called.
2140+
2141+
.. versionadded:: 5.2
2142+
2143+
The magic `magic_get` option was introduced in Symfony 5.2.
2144+
2145+
magic_set
2146+
.........
2147+
2148+
**type**: ``boolean`` **default**: ``true``
2149+
2150+
When enabled, the ``property_accessor`` service uses PHP's
2151+
:ref:`magic __set() method <components-property-access-writing-to-objects>` when
2152+
its ``setValue()`` method is called.
2153+
2154+
.. versionadded:: 5.2
2155+
2156+
The magic `magic_set` option was introduced in Symfony 5.2.
2157+
21302158
throw_exception_on_invalid_index
21312159
................................
21322160

0 commit comments

Comments
 (0)