php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #66249 empty() returns true, when it shouldn't on an object
Submitted: 2013-12-09 20:32 UTC Modified: 2013-12-09 22:32 UTC
From: kenyon dot jh at gmail dot com Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.4.22 OS: Debian
Private report: No CVE-ID: None
 [2013-12-09 20:32 UTC] kenyon dot jh at gmail dot com
Description:
------------
My PHP version wasn't an option, so it is actually "PHP Version 5.4.4-14+deb7u5" and my system is Linux toshiba 3.2.0-4-amd64 #1 SMP Debian 3.2.51-1 x86_64

Basically, when referencing an variable through a class (I only tested with __set and __get) it empty() returns true, even when it should return false.

Since it is kind of hard to explain, I posted a script below that demonstrates what I am talking about.

Test script:
---------------
<?php
class testing {
    protected $_response = array();
    
    public function __set($key, $value) {
        $this->_response[$key] = $value;
    }
    
    public function __get($key) {
        return $this->_response[$key];
    }
}

$testing = new testing();
$testing->foo = 'bar';
var_dump($testing->foo); //Returns 'string(3) "bar"
var_dump(empty($testing->foo)); //Returns bool(true)
?>

Expected result:
----------------
Line 17 should return bool(false)

Actual result:
--------------
Line 17 actually returns bool(true)

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2013-12-09 20:48 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2013-12-09 20:48 UTC] requinix@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

You need to implement __isset() too.

In general __get, __set, and __isset come as a triplet: if you have one then you should have all three.
 [2013-12-09 22:15 UTC] kenyon dot jh at gmail dot com
I see, my bad. Is there any way to clarify that in the php.net/empty documentation? That was what I was using as a reference, and why I was a little confused.

Sorry about that, thanks!
 [2013-12-09 22:24 UTC] requinix@php.net
It kinda does, actually ;)

"Note:
When using empty() on inaccessible object properties, the __isset() overloading method will be called, if declared."
 [2013-12-09 22:32 UTC] kenyon dot jh at gmail dot com
Found it. My bad haha
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu May 16 12:01:32 2024 UTC