Skip to content

Commit 4e055b9

Browse files
javiereguiluzGuikingone
authored andcommitted
Documented the enableExceptionOnInvalidIndex() method
1 parent 5f728e8 commit 4e055b9

File tree

1 file changed

+17
-1
lines changed

1 file changed

+17
-1
lines changed

components/property_access.rst

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,23 @@ method. This is done using the index notation that is used in PHP::
4747
var_dump($propertyAccessor->getValue($person, '[first_name]')); // 'Wouter'
4848
var_dump($propertyAccessor->getValue($person, '[age]')); // null
4949

50-
As you can see, the method will return ``null`` if the index does not exists.
50+
As you can see, the method will return ``null`` if the index does not exist.
51+
But you can change this behavior with the
52+
:method:`Symfony\\Component\\PropertyAccess\\PropertyAccessorBuilder::enableExceptionOnInvalidIndex`
53+
method::
54+
55+
// ...
56+
$propertyAccessor = PropertyAccess::createPropertyAccessorBuilder()
57+
->enableExceptionOnInvalidIndex()
58+
->getPropertyAccessor();
59+
60+
$person = array(
61+
'first_name' => 'Wouter',
62+
);
63+
64+
// instead of returning null, the code now throws an exception of type
65+
// Symfony\Component\PropertyAccess\Exception\NoSuchIndexException
66+
$value = $propertyAccessor->getValue($person, '[age]');
5167

5268
You can also use multi dimensional arrays::
5369

0 commit comments

Comments
 (0)