php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #77476 Different behavior of empty as of 7.0.6
Submitted: 2019-01-17 07:47 UTC Modified: 2019-02-12 15:35 UTC
Votes:1
Avg. Score:3.0 ± 0.0
Reproduced:0 of 0 (0.0%)
From: php at sandstrom dot im Assigned:
Status: Open Package: Documentation problem
PHP Version: >= 7.0.6 OS:
Private report: No CVE-ID: None
Have you experienced this issue?
Rate the importance of this bug to you:

 [2019-01-17 07:47 UTC] php at sandstrom dot im
Description:
------------
---
From manual page: https://php.net/migration70.changed-functions
---

Usage of the empty function in combination with magic methods that in PHP 5.6 returns false will return true in PHP 7.

The cause is most likely the introduction of the null coalescing operator which causes the empty function to bail early.

When running the test script on PHP 5.6 it will output

Foo::__get(foo)
Foo::__isset(bar)
Foo::__get(bar)
is not empty

and when running on PHP 7 the output will be

Foo::__isset(foo)
is empty

Test script:
---------------
<?php
class Foo {
    public function __get($name) {
        echo __METHOD__ . "($name)" . PHP_EOL;
        switch ($name) {
            case 'foo': return new Foo();
            case 'bar': return 'bar';
            default: return null;
        }
    }

    public function __isset($name) {
        echo __METHOD__ . "($name)" . PHP_EOL;
        return $name === 'bar';
    }
}

$foo = new Foo();
echo (empty($foo->foo->bar) ? 'is empty' : 'is not empty') . PHP_EOL;



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-01-17 13:29 UTC] girgias@php.net
-Summary: empty should be in the list of changed functions +Summary: Different behavior of empty as of 7.0.6 -Status: Open +Status: Feedback -PHP Version: Irrelevant +PHP Version: >= 7.0.6 -Assigned To: +Assigned To: cmb
 [2019-01-17 13:29 UTC] girgias@php.net
Using 3v4l.org it seems that this behavior was present prior to PHP 5.1.0 (so from PHP 5.0.2 to 5.0.5) and only has this behavior again as of PHP 7.0.6
c.f. https://3v4l.org/c2Xqr

So I don't think this should go into the changed functions as it wasn't changed in the initial release of PHP 7.0 per se

However, it's true that this odd behavior may need to be documented.

Any idea Christoph what to do (or whom to refer this)?
 [2019-01-17 13:48 UTC] cmb@php.net
-Status: Feedback +Status: Open
 [2019-01-17 13:48 UTC] cmb@php.net
Indeed, this looks like a bug fix (maybe 71359?), which we usually
don't document explicitly.  Considering @nikic's related
comment[1], we may want to add changelog notices in this case,
though.

[1] <https://bugs.php.net/bug.php?id=69659#1462287177>
 [2019-01-17 13:57 UTC] cmb@php.net
-Status: Assigned +Status: Open -Assigned To: cmb +Assigned To:
 [2019-02-12 15:35 UTC] girgias@php.net
Should the changelog notice be on the empty function or in the migration guide?
 
PHP Copyright © 2001-2019 The PHP Group
All rights reserved.
Last updated: Sat Feb 23 00:01:25 2019 UTC