#28176 [Opn->Sus]: isset() returns false for properties returned from __get()
| From: | helly@php.net | Date: | Mon, 10 May 2004 01:15:04 +0000 |
| Subject: | #28176 [Opn->Sus]: isset() returns false for properties returned from __get() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-59450@lists.php.net to get a copy of this message | ||
ID: 28176
Updated by: helly@php.net
Reported By: benjcarson at digitaljunkies dot ca
-Status: Open
+Status: Suspended
-Bug Type: Zend Engine 2 problem
+Bug Type: Feature/Change Request
-Operating System: Linux
+Operating System: *
-PHP Version: 5CVS-2004-04-27 (dev)
+PHP Version: 5.0.*
New Comment:
We'd need a new handler. Say __exists($name). But not before 5.0 is
out.
Previous Comments:
------------------------------------------------------------------------
[2004-04-27 17:30:37] benjcarson at digitaljunkies dot ca
It may come as no surprise, but I figure I should also mention it here
for completeness: empty() displays the same behaviour--it does not call
__get() either and returns true for variables that are set and
retrieved via __get().
Obviously I agree with amt at php.net and think these are bugs. For
now, I'm working around this with:
!is_null($f->key);
From a user's point of view, the fact that this works and
isset()/empty() do not is inconsistent. Of course, knowing that
isset()/empty() are language constructs and is_null() a function
explains the behaviour, but I still think it is incorrect.
------------------------------------------------------------------------
[2004-04-27 08:42:15] amt@php.net
Hum. I think it's a good idea to modify isset() to call
__get() and __set().
Otherwise, it breaks the principle of encapsulation. As
a user of the class, I'm not supposed to know/care if
I'm accessing the properties directly or if the
developer has overloaded the property handler.
Besides, how else as a user can I check if a property is
set if I can't use isset()?
------------------------------------------------------------------------
[2004-04-27 08:31:59] derick@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
This is correct behavior, isset() does not call the __get and __set
handlers.
------------------------------------------------------------------------
[2004-04-27 03:57:42] benjcarson at digitaljunkies dot ca
Description:
------------
isset() returns false for variables that are returned using the __get()
method, even though the variables are set and their values are returned
properly.
Reproduce code:
---------------
<?php
class Foo { var $arr; function __get($var) { return $this->arr[$var]; }
}
class Bar { var $var; function __get($tmp) { return $this->var; } }
$f = new Foo();
$f->arr["key"] = "val";
var_dump($f->key); // Value returned correctly
var_dump(isset($f->key)); // isset() disagrees
$b = new Bar();
$b->var = "blah";
var_dump($b->dummy); // Value returned correctly
var_dump(isset($b->dummy)); // isset() returns false
?>
Expected result:
----------------
string(3) "val"
bool(true)
string(4) "blah"
bool(true)
Actual result:
--------------
string(3) "val"
bool(false)
string(4) "blah"
bool(false)
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=28176&edit=1