php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #28176 isset() returns false for properties returned from __get()
Submitted: 2004-04-27 03:57 UTC Modified: 2006-04-03 12:43 UTC
Votes:3
Avg. Score:5.0 ± 0.0
Reproduced:3 of 3 (100.0%)
Same Version:2 (66.7%)
Same OS:2 (66.7%)
From: benjcarson at digitaljunkies dot ca Assigned:
Status: Closed Package: Feature/Change Request
PHP Version: 5.0.* OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: benjcarson at digitaljunkies dot ca
New email:
PHP Version: OS:

 

 [2004-04-27 03:57 UTC] 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)

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-04-27 08:31 UTC] 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 08:42 UTC] 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 17:30 UTC] 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-05-10 03:15 UTC] helly@php.net
We'd need a new handler. Say __exists($name). But not before 5.0 is out.
 [2006-04-03 12:43 UTC] tony2001@php.net
We do have them since 5.1:
bool __isset ( string name )
void __unset ( string name )
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Sep 21 00:01:27 2024 UTC