8000 minor #16630 [ExpressionLanguage] Add support for Nullsafe syntax for… · symfony/symfony-docs@e92c704 · GitHub
[go: up one dir, main page]

Skip to content

Commit e92c704

Browse files
committed
minor #16630 [ExpressionLanguage] Add support for Nullsafe syntax for accessing object's properties and methods (Sofien NAAS)
This PR was submitted for the 5.4 branch but it was merged into the 6.1 branch instead. Discussion ---------- [ExpressionLanguage] Add support for Nullsafe syntax for accessing object's properties and methods [ExpressionLanguage] [NEW FEATURE] This PR introduces the support for `nullsafe operator` in expressions that works with accessing object's properties and methods. The proposed change here, includes a paragraph under the sub-page `/expression_language/syntax` to describe the new feature usage. The sub-paragraph has a title of `Nullsafe operator` under the paragraph `Working with Objects`. The actual work related to this Doc PR available as Symfony PR [#45795](symfony/symfony#45795). Commits ------- 23bf15d [ExpressionLanguage] Add support for Nullsafe syntax for accessing object's properties and methods.
2 parents f153ae4 + 23bf15d commit e92c704

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

components/expression_language/syntax.rst

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,42 @@ JavaScript::
102102

103103
This will print out ``Hi Hi Hi!``.
104104

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

107143
Working with Functions

0 commit comments

Comments
 (0)
30A7
0