php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #77605 Incorrect behavior of empty() function is
Submitted: 2019-02-11 19:14 UTC Modified: 2019-02-11 19:16 UTC
From: ezepovarthur at gmail dot com Assigned:
Status: Not a bug Package: *General Issues
PHP Version: 7.2Git-2019-02-11 (Git) OS: CentOS Linux release 7.6.1810
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: ezepovarthur at gmail dot com
New email:
PHP Version: OS:

 

 [2019-02-11 19:14 UTC] ezepovarthur at gmail dot com
Description:
------------
PHP 7.2.14 (cli) (built: Jan  8 2019 14:05:15) ( NTS )

[PHP Modules]
apcu
bcmath
bz2
calendar
Core
ctype
curl
date
dom
exif
fileinfo
filter
ftp
gd
geoip
gettext
hash
iconv
igbinary
imagick
intl
json
ldap
libxml
mbstring
mcrypt
memcached
mongodb
msgpack
mysqli
mysqlnd
openssl
pcntl
pcre
PDO
pdo_mysql
pdo_sqlite
Phar
posix
readline
redis
Reflection
session
shmop
SimpleXML
soap
sockets
SPL
sqlite3
standard
sysvmsg
sysvsem
sysvshm
tokenizer
wddx
xdebug
xml
xmlreader
xmlrpc
xmlwriter
xsl
Zend OPcache
zip
zlib

[Zend Modules]
Xdebug
Zend OPcache

Script:

we do not have in the test script an exception, since empty() says, in this particular case, that randomProperty of Consumer instance is empty, meanwhile in_array() works correctly and goes through an array [1,2]. 

Test script:
---------------
class BaseEntity
{
    /**
     * @var array
     */
    protected $globalStack = [];

    /**
     * @param $arg
     * @param $value
     */
    public function __set($arg, $value): void
    {
        if (property_exists($this, $arg)) {
            $this->{$arg} = $value;
        } else {
            $this->globalStack[$arg] = $value;
        }
    }

    /**
     * @param $arg
     * @return mixed|null
     */
    public function __get($arg)
    {
        if (property_exists($this, $arg) && isset($this->{$arg})) {
            $value = $this->{$arg};
        } elseif (isset($this->globalStack[$arg])) {
            $value = $this->getGlobalStackValue($arg);
        } else {
            $value = null;
        }

        return $value;
    }

    public function getGlobalStackValue($arg)
    {
        return $this->globalStack[$arg];
    }
}

class Consumer extends BaseEntity
{}

$consumer = new Consumer();
$consumer->randomProperty = [1,2];
if (!empty($consumer->randomProperty) && !in_array(4, $consumer->randomProperty)) {
     throw new \Exception('whatever');
}


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-02-11 19:16 UTC] requinix@php.net
-Status: Open +Status: Not a bug -Package: Performance problem +Package: *General Issues
 [2019-02-11 19:16 UTC] requinix@php.net
Implement __isset.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 10:01:31 2024 UTC