Skip to content

Commit 24816e5

Browse files
committed
minor symfony#10095 Documented the enableExceptionOnInvalidIndex() method (javiereguiluz)
This PR was merged into the 2.8 branch. Discussion ---------- Documented the enableExceptionOnInvalidIndex() method Fixes symfony#9747. Commits ------- a59cbd2 Documented the enableExceptionOnInvalidIndex() method
2 parents 2aad13d + a59cbd2 commit 24816e5

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
@@ -51,7 +51,23 @@ method. This is done using the index notation that is used in PHP::
5151
var_dump($propertyAccessor->getValue($person, '[first_name]')); // 'Wouter'
5252
var_dump($propertyAccessor->getValue($person, '[age]')); // null
5353

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

5672
You can also use multi dimensional arrays::
5773

0 commit comments

Comments
 (0)