Re: Detecting if a null variable exists?

From: Date: Sat, 12 Jun 2010 11:07:23 +0000
Subject: Re: Detecting if a null variable exists?
References: 1 2  Groups: php.internals 
Request: Send a blank email to internals+get-48750@lists.php.net to get a copy of this message
Hello !
Is it just me or are we missing a way in the language to check if a variable that has been set to NULL exists or not? is_null() on an unset variable throws a NOTICE. $var === null throws a notice. So, you have to use isset()? But, ah, $var = null; if(isset($var)) yields false. Is array_key_exists("var", $GLOBALS) the only solution to this problem?
This question creeps up every once in a while, and I have seen bunchload of code that uses array_key_exists("foo", $array); rather then isset($array["foo"]) because of it. I however have never seen a proper usecase.. class foo {
    public function __call($method, $arguments)
    {
        // We MUST have at least ONE argument !
        if (array_key_exists(0, $arguments) === false) // isset($arguments[0]) return false if first argument has null value
        {
            trigger_error('First argument of method ' . $method . '() must be defined', E_USER_ERROR);
        }
        ...
    }
} $foo = new foo(); $foo->magicCallToMethod(null); // no error $foo->magicCallToMethod(); // error Best regards, Fred -- ======================================================================== Frédéric Hardy : Architecte d'application/Admin. système/Ergonome ========================================================================

« previous php.internals (#48750) next »