6

I have the following code in numerous places (thousands of places) around my project:

$foo = isset($mixed) ? $mixed : null;

Where $mixed can be anything: array, array element, object, object property, scalar, etc. For example:

$foo = isset($array['element']) ? $array['element'] : null;
$foo = isset($nestedArray['element']['key']) ? $nestedArray['element']['key'] : null;
$foo = isset($object->prop) ? $object->prop : null;
$foo = isset($object->chain->of->props) ? $object->chain->of->props : null;

Is there a way to write this repeated logic as a (simple) function? For example, I tried:

function myIsset($mixed)
{
    return isset($mixed) ? $mixed : null;
}

The above function looks like it would work, but it does not in practice. For example, if $object->prop does not exist, and I call myIsset($object->prop)), then I get fatal error: Undefined property: Object::$prop before the function has even been called.

Any ideas on how I would write such a function? Is it even possible?

I realize some solutions were posted here and here, but those solutions are for arrays only.

Community
  • 1
  • 1
Leo
  • 265
  • 3
  • 9
  • 4
    I don't think you can do that more efficiently. Instead I would try to reduce the number of times you use it, *thousands* seems far too much. I would guess that whole blocks of variables are either set or not so checking for one could give you the opportunity to set various. – jeroen Jan 12 '15 at 22:55
  • 3
    @jeroen It is a "code smell" if you're having to check this so often everywhere – Ruan Mendes Jan 12 '15 at 22:57

6 Answers6

11

PHP 7 has a new "Null coalescing operator" which does exactly this. It is a double ?? such as:

$foo = $mixed ?? null;

See http://php.net/manual/en/migration70.new-features.php

Paul
  • 669
  • 8
  • 9
9

I stumbled across the answer to my own question while reading about php references. My solution is as follows:

function issetValueNull(&$mixed)
{
    return (isset($mixed)) ? $mixed : null;
}

Calls to this function now look like:

$foo = issetValueNull($array['element']);
$foo = issetValueNull($nestedArray['element']['key']);
$foo = issetValueNull($object->prop);
$foo = issetValueNull($object->chain->of->props);

Hopefully this helps anyone out there looking for a similar solution.

Leo
  • 265
  • 3
  • 9
2

isset is a language construct, not a regular function. Therefore, it can take what would otherwise cause an error, and just return false.

When you call myIsset($object->prop)), the evaluation occurs and you get the error.

See http://php.net/manual/en/function.isset.php

This is the same problem as using typeof nonExistentVariable in JavaScript. typeof is a language construct and will not cause an error.

However, if you try to create a function, you get an error for trying to use an undefined variable.

function isDef(val) {
    return typeof val !== 'undefined';
}

console.log( typeof nonExistent !== 'undefined'); // This is OK, returns false
isDef(nonExistent); // Error nonExistent is not defined
Ruan Mendes
  • 90,375
  • 31
  • 153
  • 217
-1

You could actually just write it like:

$foo = $mixed?:null; 
rm-vanda
  • 3,122
  • 3
  • 23
  • 34
-2

If you just want to check if it exist do this

function myIsset($mixed)
{
    return isset($mixed); // this is a boolean so it will return true or false
}
Rafael
  • 7,605
  • 13
  • 31
  • 46
-2
function f(&$v)
{
    $r = null;

    if (isset($v)) {
        $r = $v;
    }

    return $r;
}
smottt
  • 3,272
  • 11
  • 37
  • 44
ismavolk
  • 115
  • 2
  • 3