Skip to content

Commit cbca2b4

Browse files
[ExpressionLanguage] Add support for Nullsafe syntax for accessing object's properties and methods.
1 parent 833f1b5 commit cbca2b4

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

components/expression_language/syntax.rst

+36
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,42 @@ JavaScript::
9494

9595
This will print out ``Hi Hi Hi!``.
9696

97+
Nullsafe operator
98+
~~~~~~~~~~~~~~~~~
99+
100+
Working with mutable objects can be, sometimes, error prone.
101+
The ``?.`` syntax can help avoid unnecessary and redundant checks
102+
when trying to access properties or methods on an object with dynamic structure::
103+
104+
class Apple
105+
{
106+
public $variety;
107+
}
108+
109+
$apple = new Apple();
110+
$apple->variety = 'Honeycrisp';
111+
112+
var_dump($expressionLanguage->evaluate(
113+
'fruit?.color',
114+
[
115+
'fruit' => $apple,
116+
]
117+
));
118+
119+
This will print out ``null`` instead of throwing an ``Exception``.
120+
121+
Similarly::
122+
123+
var_dump($expressionLanguage->evaluate(
124+
'fruit?.eatMe()',
125+
[
126+
'fruit' => $apple,
127+
]
128+
));
129+
130+
Will print out ``null`` since the method ``eatMe()`` we trying to access
131+
on the same ``Apple`` object does not exist.
132+
97133
.. _component-expression-functions:
98134

99135
Working with Functions

0 commit comments

Comments
 (0)