Req #63713 [Com]: Need an "UNDEFINED" type

From: Date: Wed, 31 Jul 2013 02:41:43 +0000
Subject: Req #63713 [Com]: Need an "UNDEFINED" type
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-180600@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=63713&edit=1

 ID:                 63713
 Comment by:         metamarkers at gmail dot com
 Reported by:        liquid_nitrogen_4ever at yahoo dot com
 Summary:            Need an "UNDEFINED" type
 Status:             Wont fix
 Type:               Feature/Change Request
 Package:            Variables related
 PHP Version:        Irrelevant
 Block user comment: N
 Private report:     N

 New Comment:

True, but checking for nested indices in arrays is a huge pain and often results in code
duplication.

$x = [];

Implicit array expansion is great:

$x['a']['b']['c'] = 1; // works

Example where this would be useful is the ternary operator:

return isset($x['very']['dimensional']['element']) ?
$x['very']['dimensional']['element'] : $default;

Why not:

return $x['very']['dimensional']['element'] ? : $default;

I could see this working for objects, too. Basically any undefined symbol would resolve to
UNDEFINED.

IMO since PHP doesn't throw native exceptions for nonexistent indices, UNDEFINED should be
supported. Otherwise PHP 
should throw an out of bounds exception instead of crashing, and the following code should work:

try {
    echo $x['x']['y']['z']; // doesn't exist
}
catch (Exception $e) {
    echo $e->getMessage();
}


Previous Comments:
------------------------------------------------------------------------
[2012-12-07 01:51:00] aharvey@php.net

I don't see much value in this: it's an unusual use case, would require engine changes,
and you can already check if a variable is defined with
array_key_exists('notExistentVariable', get_defined_vars()).

------------------------------------------------------------------------
[2012-12-06 17:42:43] liquid_nitrogen_4ever at yahoo dot com

Description:
------------
Consider the following scenario currently in PHP:

var_dump($notExistentVariable);
-> NOTICE: Undefined variable notHere on line 1
   NULL

isset($notExistentVariable);
-> FALSE

$foo = null;
var_dump($foo);
-> NULL

isset($foo);
-> FALSE


If there were an UNDEFINED "type" (in the same sense that there is a NULL), the 
above scenarios would change to:

var_dump($notExistentVariable);
-> UNDEFINED

isset($notExistentVariable);
-> FALSE

$foo = null;
var_dump($foo);
-> NULL

isset($foo);
-> TRUE

However, unlike NULL, you would NOT be allowed to explicitly initialize 
something to UNDEFINED.
$x=UNDEFINED;//error:  if you want it to be undefined, don't declare it.

However, having just:
$x;

makes sense (to me at least) as shortcut to:
$x=NULL;

In other words, if a variable is declared but not explicitly initialized, it 
will implicitly be set to NULL (which is 
essentially what you are already doing, but one can't really differentiate 
between declared and undeclared variables).



------------------------------------------------------------------------



-- 
Edit this bug report at https://bugs.php.net/bug.php?id=63713&edit=1


Thread (3 messages)

« previous php.bugs (#180600) next »