return NULL; vs. return; [duplicate] - php

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.

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.

Writing a generator function in php generating no elements [duplicate]

This question already has answers here:
How to pass in an empty generator parameter?
(4 answers)
Closed 4 years ago.
I can write a generator returning a single element:
function MyGen() {
yield 1;
}
In a derived class I want to write a generator that never returns any element:
function MyGen() {
}
An empty function is not a generator and will fail if passed as aforeach argument.
So - How can I write a generator which will always return no elements?
This seems to work (php7)
function MyGen() {
yield from [];
}
See http://php.net/manual/en/language.generators.syntax.php

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.

How to safely chain methods? [duplicate]

This question already has answers here:
Is there a "nullsafe operator" in PHP?
(3 answers)
Closed 2 years ago.
Is there a way to "safely" chain methods in PHP and simply return null if some previous method returns null? Otherwise, an error would be thrown: trying to get property on non-object;
For example, the following code checks whether a customer's phone number has changed using a QuickBooks SDK. I don't have control over these methods.
$customer->getPrimaryPhone() will always return an object since the form wouldn't have been submitted otherwise, but $old->getPrimaryPhone() may return null if no phone number existed previously.
The following is required to get the phone number:
$old->getPrimaryPhone()->getFreeFormNumber()
If getPrimaryPhone() returns null, then an error would be thrown.
My question is: How would I avoid code repition in the following case?
if (!empty($old->getPrimaryPhone())) {
if ($customer->getPrimaryPhone()->getFreeFormNumber() !== $old->getPrimaryPhone()->getFreeFormNumber()) {
// Repetive code here
}
} else {
// Repetive code here
}
I'd be inclined to implement an equals method on your PhoneNumber class (or whatever it's called). For example
public function equals(PhoneNumber $otherNumber) {
return $otherNumber !== null && $this->getFreeFormNumber() === $otherNumber->getFreeFormNumber();
}
Then you can simply use
if (!$customer->getPrimaryPhone()->equals($old->getPrimaryPhone())
If you've got other logic that needs to be applied (as indicated in your comment about arrays), you can easily implement that in the equals method.

finding the name of method i am in [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
get current class and method?
How can i find the name of the method i am using in php? I found how to do this in C but not in PHP. I found a Q on here which roughly talked about magic constants (here) but I didn't really get it. In the following example I want $thisMethodName to be 'model_databaseLogin'
EG:
public function model_databaseLogin()
{
$thisMethodName = ... ;
return $this->model_methodCheck( $thisMethodName );
}
Is this possible in php?
You need the "magic constant" __METHOD__. The magic constant docs should be helpful.
So your code would be:
public function model_databaseLogin() {
$thisMethodName = __METHOD__;
return $this->model_methodCheck($thisMethodName);
}
The simplest answer is the magic constants to which you refer; specifically __FUNCTION__
These are called "magic" because their value is actually contextually dynamic.
public function model_databaseLogin()
{
$thisMethodName = __FUNCTION__;
return $this->model_methodCheck( $thisMethodName );
}
There is another way, via debug_backtrace(), but that is decidedly less efficient!

Categories