php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24915 empty()/isset() misleading with __get/__set
Submitted: 2003-08-02 07:12 UTC Modified: 2003-11-29 09:54 UTC
Votes:12
Avg. Score:4.7 ± 0.6
Reproduced:11 of 11 (100.0%)
Same Version:5 (45.5%)
Same OS:5 (45.5%)
From: tater at potatoe dot com Assigned:
Status: Wont fix Package: Scripting Engine problem
PHP Version: 5CVS-2003-11-29 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: tater at potatoe dot com
New email:
PHP Version: OS:

 

 [2003-08-02 07:12 UTC] tater at potatoe dot com
Description:
------------
Given a "property" that is really being handled by __get() and __set() functions, you are allowed to use it with empty() and isset() without errors or warnings, but they always report that the property is empty/not-set.

I understand that this may not be a bug, but a "feature"
of PHP 5 - i.e., just the way it works - but if that is true,
please be kind enough to say so explicitly. It is not helpful
to mark bugs as Bogus with comments like "I suggest you read ZEND_CHANGES :)"

If it is to be expected, it might be good to throw the
same kind of error that one would see if trying to use 
empty() on a function call, if that's possible.

This is possibly related to bug #24436.

Reproduce code:
---------------
class foo
{
    private $_x;
    private function __get($p) { return $this->_x; }
    private function __set($p,$v) { $this->_x = $v; }
}
$y = new foo;
$y->x = 12;
if (empty($y->x))
    print "y->x is empty: {$y->x} \n";
else
    print "y->x is not empty: {$y->x} \n";
if (isset($y->x))
    print "y->x is set: {$y->x} \n";
else
    print "y->x is not set: {$y->x} \n";

Expected result:
----------------
y->x is not empty: 12 
y->x is set: 12

Actual result:
--------------
y->x is empty: 12 
y->x is not set: 12

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-11-29 00:18 UTC] tater at potatoe dot com
Oh, I see, you just saw the word "private" and threw a mental exception. OK, here:

class foo
{
    public $_x;
    public function __get($p) { return $this->_x; }
    public function __set($p,$v) { $this->_x = $v; }
}
$y = new foo;
$y->x = 12;
if (empty($y->x))
    print "y->x is empty: {$y->x} \n";
else
    print "y->x is not empty: {$y->x} \n";
if (isset($y->x))
    print "y->x is set: {$y->x} \n";
else
    print "y->x is not set: {$y->x} \n";
 [2003-11-29 09:54 UTC] helly@php.net
There are no plans on adding the missing third handler.
BUT you can go with the newly introduced ArrayAccess interface which supports all necessary access methods.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Apr 26 15:01:28 2025 UTC