[PHP-BUG] Req #71254 [NEW]: Allow limited recursion with __get().

[email protected] ("andreas at dqxtech dot net")
Newsgroups php.bugs
Message-ID <[email protected]>
From:             andreas at dqxtech dot net
Operating system: 
PHP version:      7.0.2RC1
Package:          Class/Object related
Bug Type:         Feature/Change Request
Bug description:Allow limited recursion with __get().

Description:
------------
Currently, magic __get() refuses to dive into recursion if the same key
is repeated.
https://3v4l.org/00Kuv

It stops, and gives a "Notice: Undefined property: C::$x".

There are cases where the recursion is not a bug, and it is indeed
legitimate to dive one level into the recursion.

As we can see, calling ->__get('x') explicitly instead of magic ->x,
avoids the notice. But this is undesirable, because we want the IDE to
recognize that we are calling @property $x.

The unlimited recursion can be avoided with stubs:
https://3v4l.org/Nb3VF

Proposal:
I want to propose that the recursion detection mechanic only gets active
if the same key is used a 3rd time, not the 2nd time. This permits the
legitimate case, but still prevents unlimited recursion.

Test script:
---------------
<?php
class C {
    private $buffer = array();
    function __get($key) {
        if (array_key_exists($key, $this->buffer)) {
            return $this->buffer[$key];
        }
        $f = 'calc_' . $key;
        return $this->buffer[$key] = $this->$f();
    }
    function calc_x() {
        $x = new stdClass;
        // Set a stub to avoid infinite recursion.
        $this->buffer['x'] = $x;
        $x->y = $this->y;
        return $x;
    }
    function calc_y() {
        $y = new stdClass;
        // Set a stub to avoid infinite recursion.
        $this->buffer['y'] = $y;
        $y->x = $this->x;
        return $y;
    }
}


-- 
Edit bug report at https://bugs.php.net/bug.php?id=71254&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=71254&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=71254&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=71254&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=71254&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=71254&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=71254&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=71254&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=71254&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=71254&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=71254&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=71254&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=71254&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=71254&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=71254&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=71254&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=71254&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=71254&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=71254&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=71254&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=71254&r=mysqlcfg
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.