Skip to content

Commit 835d083

Browse files
committed
Reword
1 parent e92c704 commit 835d083

File tree

1 file changed

+13
-30
lines changed

1 file changed

+13
-30
lines changed

components/expression_language/syntax.rst

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -102,41 +102,24 @@ JavaScript::
102102

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

105-
Nullsafe operator
106-
~~~~~~~~~~~~~~~~~
105+
Null-safe Operator
106+
~~~~~~~~~~~~~~~~~~
107107

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::
108+
Use the ``?.`` syntax to access properties and methods of objects that can be
109+
``null`` (this is equivalent to the ``$object?->propertyOrMethod`` PHP null-safe
110+
operator)::
111111

112-
class Apple
113-
{
114-
public $variety;
115-
}
112+
// these will throw an exception when `fruit` is `null`
113+
$expressionLanguage->evaluate('fruit.color', ['fruit' => '...'])
114+
$expressionLanguage->evaluate('fruit.getStock()', ['fruit' => '...'])
116115

117-
$apple = new Apple();
118-
$apple->variety = 'Honeycrisp';
116+
// these will return `null` if `fruit` is `null`
117+
$expressionLanguage->evaluate('fruit?.color', ['fruit' => '...'])
118+
$expressionLanguage->evaluate('fruit?.getStock()', ['fruit' => '...'])
119119

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-
));
120+
.. versionadded:: 6.1
137121

138-
Will print out ``null`` since the method ``eatMe()`` we trying to access
139-
on the same ``Apple`` object does not exist.
122+
The null safe operator was introduced in Symfony 6.1.
140123

141124
.. _component-expression-functions:
142125

0 commit comments

Comments
 (0)