php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #68243 IMPORTANT! Checking empty value failed
Submitted: 2014-10-16 12:25 UTC Modified: 2016-12-11 04:22 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: Siemienik dot Pawel at gmail dot com Assigned: cmb (profile)
Status: No Feedback Package: *PDF functions
PHP Version: 5.5.17 OS: Ubuntu 14
Private report: No CVE-ID: None
 [2014-10-16 12:25 UTC] Siemienik dot Pawel at gmail dot com
Description:
------------
!empty((AnyClass::getInstance()->title))
 !==
!empty(AnyClass::getInstance()->title)

Test script:
---------------
var_dump(
 !empty((AnyClass::getInstance()->title)),
 !empty(AnyClass::getInstance()->title)
)
if(!empty((AnyClass::getInstance()->title)) !== empty(AnyClass::getInstance()->title))
  var_dump('BIG PROBLEM HERE!')



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2014-10-16 12:27 UTC] siemienik dot pawel at gmail dot com
int AnyClass is function __get(), which will return string value;
 [2014-10-18 16:39 UTC] keithm at aoeex dot com
Works for me

If you believe this is a problem, provide a small but complete test script (including class definitions) which will reproduce the problem.

Test Script:
-----------------------------------------------------------------
<?php

class A {
        static $instance;
        public static function getInstance(){
                if (!self::$instance){
                        self::$instance=new self;
                }
                return self::$instance;
        }

        public function __get($nm){
                return '';
        }
}

var_dump(
        !empty(A::getInstance()->title)
        , !empty((A::getInstance()->title))
        , !empty((A::getInstance()->title)) !== empty(A::getInstance()->title)
);

Result:
-----------------------------------------------------------------
bool(false)
bool(false)
bool(true)


Version:
-----------------------------------------------------------------
Linux:
PHP 5.5.9-1ubuntu4.4 (cli) (built: Sep  4 2014 06:56:34)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

Windows:
PHP 5.5.18 (cli) (built: Oct 15 2014 13:04:56)
Copyright (c) 1997-2014 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
 [2014-10-18 17:02 UTC] keithm at aoeex dot com
Forgot to try with a non-empty value, which does shows the symptom. 
From the documentation however:

-------
Note:
When using empty() on inaccessible object properties, the __isset() overloading method will be called, if declared.
-------

You need to implement __isset() if you want to check whether magic properties exist.


Explanation:
Adding the () inside the empty call means that empty checks the result of the expression, which will be the string value returned by __get.  Without the () PHP will check the if the property exists on the object instead. 

empty(A::getInstance()->title) is equivalent to 
!isset(A::getInstance()->title) || !A::getInstance()->title;

whereas 
empty((A::getInstance()->title)) is equivalent to
($var = A::getInstance()->title) && (!isset($var) || !$var);
 [2016-12-01 17:27 UTC] cmb@php.net
-Status: Open +Status: Feedback -Assigned To: +Assigned To: cmb
 [2016-12-01 17:27 UTC] cmb@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with ,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc. If the script requires a 
database to demonstrate the issue, please make sure it creates 
all necessary tables, stored procedures etc.

Please avoid embedding huge scripts into the report.
 [2016-12-11 04:22 UTC] php-bugs at lists dot php dot net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Re-Opened". Thank you.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Oct 11 00:01:28 2024 UTC