0

First a simple example:

function doReturnSomething()
{
    // some logic
    if ($resultFromLogic) {
        return Default_Model_Something;
    }

    return false;
}

As you can see, this functions returns a model or false. Now, let's call this function from some place else:

$something = doReturnSomething();

No, I want to put a if-statement to check the variable $something. Let's say:

if (false !== $something) {}

or

if ($something instanceof Default_Model_Something) {}

or

...

Is there a "best practice" for this situation to do? Check for false or use instance of. And are there any consequences for that particular way?

Thank is advance!

1
  • Not directly to the question, but I suggest to return null instead of a bollean, because a) null is the equivalent of "nothing", which seems to fit here quite well, and b) there may be situations, where the return value should be a boolean and then you cannot distinguish between "nothing" false and "value" false Commented Jan 16, 2012 at 12:37

3 Answers 3

1

Depending on what it is returning, you could just do this:

if(doReturnSomething()) {}

It will return true when an object is returned (Default_Model_Something) and return false when false is returned or a null-value. Just don't use this way when working with numbers because "0" will evaluate to false.

Sign up to request clarification or add additional context in comments.

4 Comments

This way works well with numbers, as long as (you mentioned it) 0 is not a valid value. For example IDs usually starts with 1
That's true. When using instanceof, we can be sure that particular methods / properties are available from the class Default_Model_Something. Are there any disadvantages when using instanceof? If "false !==" a more generic way of typing?
Hey, sorry if i m getting it wrong, but what if doReturnSomething() returns valid model and i have to access it inside if braces. it would be better to use it like $something = doReturnSomething(); and then if($something){}
Yes of course... when you need it on multiple locations you should store it in a variable and check the variable instead. @VincentvanDijk: instanceof also works, but it has limitations in flexibility etc. When you have to depend on certain functions however, I guess it would be best to create some sort of an interface so you can just plugin new classes.
1

I think that from PHP 5.4 on there is a special notice error for threating a function return in logical statements.

Good pratice is to save result of a function to variable and then use it in any logical statement.

1 Comment

I don't think there is such a notice. You can try here online: codepad.viper-7.com - The other part in your answer sounds good IMHO, but maybe you could tell why?
-1
<?php function my($a, $b ,$c){

    $total = $a + $b - $c;      return $total;       }

$myNumber = 0;

echo "Before the function, myNumber = ". $myNumber ."<br />";

$myNumber = my(3, 4, 1); // Store the result of mySum in $myNumber

echo "After the function, myNumber = " . $myNumber ."<br />";

?>

1 Comment

What does this have to do with the question asked?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.