note 87098 deleted from function.isset by thiago
| From: | thiago@php.net | Date: | Wed, 19 Nov 2008 12:02:32 +0000 |
| Subject: | note 87098 deleted from function.isset by thiago | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-146705@lists.php.net to get a copy of this message | ||
Note Submitter: Daniel Smedegaard Buus
----
This is terrible. We have no real isset() function in the sense that it should check if something is
SET. It is misnamed, it should be "is_set_and_not_null()", just like empty() should be
"empty_or_zero_or_false()" and nl2br() should be "prepend_nls_with_br()". The
only difference between isset() and !is_null() is that is_null() triggers an error if you try to
access undeclared members, while isset() does not.
There is no practically feasible way to check if a member of a class exists if it is set to the
value null (this is NOT the same as not being set! null is a value, not a state). Consider this:
class ClassName {
public static $state = null;
public function &__get($name) {
if (isset(self::$$name))
return self::$$name;
trigger_error('No such member', E_USER_WARNING);
return null;
}
}
$class = new ClassName();
if is_null($class->state) // Will trigger an error 'No such member'.
{
start_something(time());
$class->state = 'running';
}
This is just an example to show you what I'm talking about. Please don't start with the
"Why would you do that?" or "You should do this!", because that's not the
point.
The point is that we have no way to know if a member is really declared without doing something like
(self::$$name === null) in the magic function while temporarily running with a custom error handler
to "catch" the error that will be triggered by is_null() if it isn't declared. This
isn't exactly feasible when considering the performance issues.
Now why don't we have access to whatever construct php uses internally that is used to
determine that it's time to throw a "Fatal error: Access to undeclared static property:
ClassName::$i_do_not_exist"? This is what we need, not this is_set_and_not_null() function.