@@ -63,6 +63,9 @@ method::
63
63
// Symfony\Component\PropertyAccess\Exception\NoSuchIndexException
64
64
$value = $propertyAccessor->getValue($person, '[age]');
65
65
66
+ // You can avoid the exception by adding the nullsafe operator
67
+ $value = $propertyAccessor->getValue($person, '[age?]');
68
+
66
69
You can also use multi dimensional arrays::
67
70
68
71
// ...
@@ -101,6 +104,36 @@ To read from properties, use the "dot" notation::
101
104
102
105
var_dump($propertyAccessor->getValue($person, 'children[0].firstName')); // 'Bar'
103
106
107
+ .. tip ::
108
+
109
+ You can give an object graph with nullable object.
110
+
111
+ Given an object graph ``comment.person.profile ``, where ``person `` is optional (can be null),
112
+ you can call the property accessor with ``comment.person?.profile `` (using the nullsafe
113
+ operator) to avoid exception.
114
+
115
+ For example::
116
+
117
+ class Person
118
+ {
119
+ }
120
+ class Comment
121
+ {
122
+ public ?Person $person = null;
123
+ public string $message;
124
+ }
125
+
126
+ $comment = new Comment();
127
+ $comment->message = 'test';
128
+
129
+ // This code throws an exception of type
130
+ // Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException
131
+ var_dump($propertyAccessor->getValue($comment, 'person.firstname'));
132
+
133
+ // The code now returns null, instead of throwing an exception of type
134
+ // Symfony\Component\PropertyAccess\Exception\UnexpectedTypeException,
135
+ var_dump($propertyAccessor->getValue($comment, 'person?.firstname')); // null
136
+
104
137
.. caution ::
105
138
106
139
Accessing public properties is the last option used by ``PropertyAccessor ``.
0 commit comments