Warning: "continue" targeting switch is equivalent to "break". Did you mean to use "continue 2"? in /data/e/7/e76990b9-3372-4d88-8089-fce36e16744a/webperfection.net/sub/rady-nosu/wp-content/plugins/seo-ultimate/modules/class.su-module.php on line 1195
PHP 7 isset not working

May 9, 2016 | In: Programovanie

PHP 7.0.6 isset not working properly – anymore

Checking nested structure with isset does not work anymore!

In the project I’m working on currently, all the models extends from ArrayObject:

abstract class AbstractModel extends ArrayObject

Now, I found the following on many places (or similar-ish):


if (isset($brandModel->extra->espInformation->folders->folderItems)) {
    return $brandModel->extra->espInformation->folders->folderItems;
}

The brand model:

class Brands extends AbstractModel
{
    private $extra = 'hey!!!';
    // some more public/protected properties
}

Before, if I would check for properties inside the model with isset($this->$key), however, this is no longer working if the property is private!!! Isset is not a function so it was not throwing an error on checking not existing indexes/properties.

The code before:

    public function __isset($key)
    {
        if ($this->flag == self::ARRAY_AS_PROPS) {
            return $this->offsetExists($key);
        }

        // will return false >= php7.0.6
        return isset($this->$key);
    }

Now

    public function __isset($key)
    {
        if ($this->flag == self::ARRAY_AS_PROPS) {
            return $this->offsetExists($key);
        }

        /**
         * nasty PHP 7.0.6 "fix" #62059 && #69659
         * isset is not working properly anymore
         * and will not evaluate inner arrays or private properties
         * and thus we have to steal the private and check isset on value rather than object
         */
        $reader = function &($object, $property) {
            $value = &\Closure::bind(
                function &() use ($property) {
                    return $this->$property;
                },
                $object,
                $object
            )->__invoke();

            return $value;
        };

        $value = &$reader($this, $key);

        return isset($value);
    }

The isset is however working on protected and public, only accessing private properties is messed up.

Furthermore, they argue it is a “bugfix” (if you bother to read the issue comments), however, I think it is extremely stupid to fix such a important function in a minor release. Shame on you!!!

PS: credit for stealing cookies goes to Ocramius 😉 https://ocramius.github.io/blog/accessing-private-php-class-members-without-reflection/

Share This:

Comments are closed.