PHP question mark operator (not a ternary operator) [duplicate] - php

This question already has answers here:
What is the purpose of the question marks before type declaration in PHP7 (?string or ?int)?
(4 answers)
Closed 4 years ago.
I ran into a code in PHP, which has strange question mark operators. Since the code itself doesn't have any comments on that part, i tried to google it, but failed to succeed.
The code i'm interested in goes like:
<?php
class Cart
{
private $_user;
private $_items = [];
public function __construct(?User $user)
{
$this->_user = $user;
}
public function getUser(): ?User
{
return $this->_user;
}
My first thought was of type hinting, which was introduced in PHP 7, but it's apparently not the case, nor it is the strict type declaration. I've got no idea, what it is. Can you help me?

http://php.net/manual/en/migration71.new-features.php
Type declarations for parameters and return values can now be marked as nullable by prefixing the type name with a question mark. This signifies that as well as the specified type, NULL can be passed as an argument, or returned as a value, respectively.

This means your parameter or return can be null.
PHP 7.1 introduced it : http://php.net/manual/en/migration71.new-features.php

Related

What is the difference between "?" and "= null" in PHP function parameters [duplicate]

This question already has answers here:
What is the purpose of the question marks before type declaration in PHP7 (?string or ?int)?
(4 answers)
Closed 1 year ago.
I was doing some research but wasn't able to find an answer (probably beacause I did not searched it right)
Consider this piece of code:
public function foo(?array $optionalParam);
And then this one:
public function foo(array $optionalParam = null);
What differs between them? Using PHPstorm I noticed that when I use the ?, it creates a PHPdoc and mark the variable type as type|null. But when I call the function without that argument, PHP screams on my face "you kidding me? where is $optionalParam". In the other side, I managed to use with no problems the =null option.
Sorry if this question is too simple, but i did not find any answers online.
First of all, the ? goes before the type, not after... other than this:
Using
public function foo(?array $optionalParam);
you are forced to pass something, that can be either null or an array, infact:
<?php
function foo(?array $optionalParam){
echo "test";
}
foo(); // doesn't work
foo(null); // works
foo([]); // works
where instead using
public function foo(array $optionalParam = null);
will accept null, an array, or 0 parameters
<?php
function foo(array $optionalParam = null){
echo "test";
}
foo(null); // works
foo(); // work
foo([]); // works
It's a PHP 7.1 feature called Nullable Types
Both of the lines you wrote are identical.
array ?$optionalParam : either an array or null
array $optionalParam = null : either an array or null
Tho using ? you'd still need to add the parameter when calling the function.

What does ":?" after a method means in PHP? [duplicate]

This question already has answers here:
What is the purpose of the question marks before type declaration in PHP7 (?string or ?int)?
(4 answers)
Reference Guide: What does this symbol mean in PHP? (PHP Syntax)
(24 answers)
Closed 4 years ago.
I just saw this in a Symfony 4 application, and I can't find nowhere what it means
public function findOneBySomeField($value): ?Article
{
return $this->createQueryBuilder('a')
->andWhere('a.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
I know that, now with PHP 7 you can define the expected type of the returned value with ":int $val", but here, what does the ? symbol means ?
This is a new feature as of PHP 7.1. See the explanation here
Type declarations for parameters and return values can now be marked as nullable by prefixing the type name with a question mark. This signifies that as well as the specified type, NULL can be passed as an argument, or returned as a value, respectively.
This means the expected output of your function will be either an Instance of the class Article or it is NULL.

Is there a difference between returning void and null in PHP? [duplicate]

This question already has answers here:
Should a function use: return null;?
(7 answers)
Closed 5 years ago.
When I have a function like this:
public function getResult() {
return;
}
Is it the exactly same as returning null?
And if I'd be doing this:
is_null($this->getResult());
Would that result in true?
In terms of runtime behaviour, no, there is not. All functions implicitly return a NULL, and return with no value (so return;) produces a NULL return value.
If you use the : void return type declaration in PHP 7.1+, return NULL; (instead of return;) is forbidden, but this is really just a coding-style restriction. Function calls always produce some value in PHP, so var_dump((function (): void {})()); outputs NULL.
According to above mentioned description the following expression
is_null($this->getResult());
will evaluate to true since function in above mentioned code snippet returns void.

return NULL; vs. return; [duplicate]

This question already has answers here:
Should a function use: return null;?
(7 answers)
Closed 7 years ago.
I wonder if there are any differences in PHP.
Lets assume I have the following function(s)
public function myFunc() {
// some logic here
return;
}
and this here:
public function myFunc2() {
// some more logic here
return null;
}
I understand, that returning "" (an empty String) is something different than null. A var_dump() on each of these functions returns NULL. Is this internally (bitwise or for some comparison) somehow handled differently?
Does it affect the parsing-time? Is it just a good practice to write return NULL or is it more like a convention?
I didn't studied source code of PHP, but as a developer I'm using return null when function should return a value (by design) and simple return when I need to just leave a function which is not returning any value.

What does prepending '&' to a function name mean in PHP? [duplicate]

This question already has answers here:
What does it mean to start a PHP function with an ampersand?
(3 answers)
Closed 7 years ago.
I'm using a CMS package written in PHP. In one of it's core files I saw following line that is for defining a function in a class body.
public static function &getLib($sClass, $aParams = array()) {
// Code
}
I didn't understand why the function name 'getLib' has been prepended with the ampersand(&) sign? I've never seen such thing before.
Can someone please explain me in detail why such thing has been done and what's the benefit it has over simply using the function name?
It means the function should return a reference to a variable rather than just the value itself.

Categories