For coders TYPO3 Tech Corner

Null-Safe Operator in the TYPO3 area

Null-Safe Operator in the TYPO3 area

With the introduction of PHP8, problems with undefined arrays or variables in general can arise in many places. Here are a few examples and simple solutions.

In PHP, you're sure to come across variables that don't contain an expected object or arrays that don't have a certain key set. Various IF conditions may also have been inserted. Here are a few examples of how you can write smart code using a null-save operator.

PHP

Here is a short example of arrays:

# Old $color = ''; if (array_key_exists('color', $this->settings)) { $color = $this->settings['color']; } # New $color = $this->settings['color'] ?? '';

And here for function and property calls:

# Old $address = $customer->getAddress(); $country = null; if ($address->getCountry() !== null) { $country = $address->getCountry(); } # Not quite so old $address = $customer->getAddress(); $country = $address ? $address->getCountry() : null; # New $country = $customer->getAddress()?->getCountry(); # Alternative example $tsfe = $GLOBALS['TSFE'] ?? null; $id = (int)$tsfe?->id;
TypoScript

The topic has also already arrived in TypoScript. Problems can quickly arise, especially when using conditions. Here is an example when $GLOBALS['TSFE'] is not available (e.g. in the backend or CLI context):

# Old [getTSFE().type == 123] # New [getTSFE()?.type == 123]
JavaScript

We have one more. In JavaScript you can also avoid unnecessary IF conditions with a null-save operator:

# Old let myValue = ''; const element = document.querySelector('[data-foo]'); if (element !== null) { myValue = element. getAttribute('data-foo'); } # New let myValue = document.querySelector('[data-foo]')?.getAttribute('data-foo');
Alexander Kellner

Alex Kellner

Alex Kellner is not only known for his many TYPO3 extensions such as powermail, femanager or lux, but also for his community work. He is also happy to give administration or development training courses or workshops.

Alexander Kellner  |  Management & COO

"Code faster, look at the time" - does this sound familiar to you?

How about time and respect for code quality? Working in a team? Automated tests?

Join us

SQL: Show all tables sorted by size in descending order

Lately I've been using the SQL command more often to find out which tables in the TYPO3 database are the largest. I've published the snippet once.

Go to news

TYPO3 12 with CKEditor 5: Styles in a single selection

If you set a link in the RTE in TYPO3, you may have to choose between different link classes, for example to create buttons in the frontend. What's new in TYPO3 12 is that you can select not just one...

Go to news

Delete the first/last lines of a (SQL) file

There isn't much to say about the following commands. Sometimes it can be useful to delete the first (or last) X lines from a file. And if the file is too large to open with a conventional program, a...

Go to news

b13/container: Add and modify child elements in edit view

Unlike gridelements, you cannot manage the child elements in the B13 Container extension when you open the container in the editing view. I would be happy to show you how you can quickly install this...

Go to news

Menu comparison: Numbers, numbers, numbers

Go to news

Drop-down menu without load - using multilevel cache

Go to news