8000 [ExpressionLanguage] Add support for Nullsafe syntax for accessing object's properties and methods by mytuny · Pull Request #16630 · symfony/symfony-docs · GitHub
[go: up one dir, main page]

Skip to content

[ExpressionLanguage] Add support for Nullsafe syntax for accessing object's properties and methods #16630

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions components/expression_language/syntax.rst
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,42 @@ JavaScript::

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

Nullsafe operator
~~~~~~~~~~~~~~~~~

Working with mutable objects can be, sometimes, error prone.
The ``?.`` syntax can help avoid unnecessary and redundant checks
when trying to access properties or methods on an object with dynamic structure::

class Apple
{
public $variety;
}

$apple = new Apple();
$apple->variety = 'Honeycrisp';

var_dump($expressionLanguage->evaluate(
'fruit?.color',
[
'fruit' => $apple,
]
));

This will print out ``null`` instead of throwing an ``Exception``.

Similarly::

var_dump($expressionLanguage->evaluate(
'fruit?.eatMe()',
[
'fruit' => $apple,
]
));

Will print out ``null`` since the method ``eatMe()`` we trying to access
on the same ``Apple`` object does not exist.

.. _component-expression-functions:

Working with Functions
Expand Down
0