We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Volt substitute for php error control operator '@'

PHP supports one error control operator: the at sign (@). When prepended to an expression in PHP, any error messages that might be generated by that expression will be ignored. Is there a Volt substitute for it?

Example: <span>{{ session.get('currentUser')['firstName'] }}</span>

This is one of the worst php ideas, not sure why would you want to do it, just check if it exists.

edited May '19

Aaah c'mon Slawski, It's a pretty good idea and it saves you the trouble of extra conditions. But let's not argue about ideas and get this thaing supported, shall we?

edited May '19

Well whatever, you can create NFR, but for me it's really bad idea to implement this. You should always check things like this. Havin @ anywhere in your code is very bad.

Instead of having array you could just store object in session, and if no logged user just store empty user with some flag saying if he is logged or not.

Anyway you need to do if here anyway, because otherwise what? You just display empty span element?

edited May '19

Aaah c'mon Slawski, It's a pretty good idea and it saves you the trouble of extra conditions. But let's not argue about ideas and get this thaing supported, shall we?

Error suppression is a bad practice, and the framework should not encourage it.

Also, a template syntax should only be responsible for presentation and formatting, not application logic.

edited May '19

In that particular use case, I can think of 2 approaches. The first would be to extract the variable in the controller:

$first_name = $this->session->get('currentUser')['firstName'] ?? '';

Or you could drop down into PHP right in the template:

<span><?= $this->session->get('currentUser')['firstName'] ?? ''; ?></span>

As others have said, error suppresion is bad practice. As to why it's bad practice: You can't always know what errors are going to be generated. If you suppress errors you could be missing out on something important. Additionally, the code that gets written that causes the errors is.... causing errors. Code that causes errors is broken code.