Skip to main content
added 210 characters in body
Source Link

As of PHP 8, there is the nullsafe operator ?->. With it you could refactor your code like this:

// @var User|null $user $data['id_user'] = $user?->getId(); 

Prior to PHP 8, you could utilize automatic bool converting to take advantage of the fact that, when converting to bool, null is considered false, but other objects are not (except apparently SimpleXML objects created from empty tags are also considered false). So you could shorten your example to the following:

// @var User|null $user $data['id_user'] = $user ? $user->getId() : null; 

However, this might be less readable since the ? might imply to some people you are working with a bool to begin with, or it could make the reader have to think about how null gets converted to bool. Your original code is more explicit, and thus potentially more readable. Personally inIn order to make brains work less hard, I like to avoidoften at least consider avoiding using "not equals" whenever possible, and just go with a straight "equals". So you could refactor your code to be like this:

// @var User|null $user $data['id_user'] = $user === null ? null : $user->getId(); 

In this particular case, I think that trick (avoiding "not equals") works fairly well, but sometimes counterintuitively, the code is still more naturally readable to just use a "not equals" anyways.

As of PHP 8, there is the nullsafe operator ?->. With it you could refactor your code like this:

// @var User|null $user $data['id_user'] = $user?->getId(); 

Prior to PHP 8, you could utilize automatic bool converting to take advantage of the fact that, when converting to bool, null is considered false, but other objects are not (except apparently SimpleXML objects created from empty tags are also considered false). So you could shorten your example to the following:

// @var User|null $user $data['id_user'] = $user ? $user->getId() : null; 

However, this might be less readable since the ? might imply to some people you are working with a bool to begin with, or it could make the reader have to think about how null gets converted to bool. Your original code is more explicit, and thus potentially more readable. Personally in order to make brains work less hard, I like to avoid using "not equals" whenever possible, and just go with a straight "equals". So you could refactor your code to be like this:

// @var User|null $user $data['id_user'] = $user === null ? null : $user->getId(); 

As of PHP 8, there is the nullsafe operator ?->. With it you could refactor your code like this:

// @var User|null $user $data['id_user'] = $user?->getId(); 

Prior to PHP 8, you could utilize automatic bool converting to take advantage of the fact that, when converting to bool, null is considered false, but other objects are not (except apparently SimpleXML objects created from empty tags are also considered false). So you could shorten your example to the following:

// @var User|null $user $data['id_user'] = $user ? $user->getId() : null; 

However, this might be less readable since the ? might imply to some people you are working with a bool to begin with, or it could make the reader have to think about how null gets converted to bool. Your original code is more explicit, and thus potentially more readable. In order to make brains work less hard, I often at least consider avoiding using "not equals" whenever possible, and just go with a straight "equals". So you could refactor your code to be like this:

// @var User|null $user $data['id_user'] = $user === null ? null : $user->getId(); 

In this particular case, I think that trick (avoiding "not equals") works fairly well, but sometimes counterintuitively, the code is still more naturally readable to just use a "not equals" anyways.

Added something closer to an answer to the original question, but specifying it only works as of PHP 8
Source Link

As of PHP 8, there is the nullsafe operator ?->. With it you could refactor your code like this:

// @var User|null $user $data['id_user'] = $user?->getId(); 

Prior to PHP 8, you could utilize automatic bool converting to take advantage of the fact that, Whenwhen converting to bool, null is considered false, but other objects are not (except apparently SimpleXML objects created from empty tags are also considered false). So you could shorten your example to the following:

// @var User|null $user $data['id_user'] = $user ? $user->getId() : null; 

However, this might be less readable since the ? might imply to some people you are working with a bool to begin with, or it could make the reader have to think about how null gets converted to bool. Your original code is more explicit, and thus potentially more readable. Personally in order to make brains work less hard, I like to avoid using "not equals" whenever possible, and just go with a straight "equals". So you could refactor your code to be like this:

// @var User|null $user $data['id_user'] = $user === null ? null : $user->getId(); 

When converting to bool, null is considered false, but other objects are not (except apparently SimpleXML objects created from empty tags are also considered false). So you could shorten your example to the following:

// @var User|null $user $data['id_user'] = $user ? $user->getId() : null; 

However, this might be less readable since the ? might imply to some people you are working with a bool to begin with, or it could make the reader have to think about how null gets converted to bool. Your original code is more explicit, and thus potentially more readable. Personally in order to make brains work less hard, I like to avoid using "not equals" whenever possible, and just go with a straight "equals". So you could refactor your code to be like this:

// @var User|null $user $data['id_user'] = $user === null ? null : $user->getId(); 

As of PHP 8, there is the nullsafe operator ?->. With it you could refactor your code like this:

// @var User|null $user $data['id_user'] = $user?->getId(); 

Prior to PHP 8, you could utilize automatic bool converting to take advantage of the fact that, when converting to bool, null is considered false, but other objects are not (except apparently SimpleXML objects created from empty tags are also considered false). So you could shorten your example to the following:

// @var User|null $user $data['id_user'] = $user ? $user->getId() : null; 

However, this might be less readable since the ? might imply to some people you are working with a bool to begin with, or it could make the reader have to think about how null gets converted to bool. Your original code is more explicit, and thus potentially more readable. Personally in order to make brains work less hard, I like to avoid using "not equals" whenever possible, and just go with a straight "equals". So you could refactor your code to be like this:

// @var User|null $user $data['id_user'] = $user === null ? null : $user->getId(); 
Source Link

When converting to bool, null is considered false, but other objects are not (except apparently SimpleXML objects created from empty tags are also considered false). So you could shorten your example to the following:

// @var User|null $user $data['id_user'] = $user ? $user->getId() : null; 

However, this might be less readable since the ? might imply to some people you are working with a bool to begin with, or it could make the reader have to think about how null gets converted to bool. Your original code is more explicit, and thus potentially more readable. Personally in order to make brains work less hard, I like to avoid using "not equals" whenever possible, and just go with a straight "equals". So you could refactor your code to be like this:

// @var User|null $user $data['id_user'] = $user === null ? null : $user->getId();