php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #31976 Accessing overloaded member via foreach does not work
Submitted: 2005-02-15 02:00 UTC Modified: 2005-02-15 10:39 UTC
From: adove at booyahnetworks dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.0.3 OS: WinXP
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: adove at booyahnetworks dot com
New email:
PHP Version: OS:

 

 [2005-02-15 02:00 UTC] adove at booyahnetworks dot com
Description:
------------
Using an overloaded property from an object instance directly in a foreach fails. If you assign the property (or a reference to it) to a variable first it works fine. This is reproducable. 

Interestingly enough, I see a strange problem with inheritance and overloaded members. I am unable  to reproduce it in a simple example. I will continue to work that. Basically, you get the same fatal error as here BUT on assignment to a member variable of the parent class that is not overloaded. It's bizzare. As soon as I remove the __get/__set from the child, the parent method works fine again from an instance of child. Again, simple examples do not seem to reproduce. <sigh>

Reproduce code:
---------------
class Son
{
    protected $m_aActions;
    
    function __construct(&$aActions)
    {
        $this->m_aActions = $aActions;
    }
    
    function __get($mName)
    {
        $mRetval = null;
        
        switch($mName)
        {
            case("Actions"):
            {
                $mRetval = $this->m_aActions;
                break;
            }
        }
        
        return $mRetval;
    }
}

$aActions = array("add", "delete");
$oSon = new Son($aActions);

$aActions = $oSon->Actions;
var_dump($aActions);

foreach($oSon->Actions as $strAction)
{
    echo $strAction . "\n";
}



Expected result:
----------------
array(2) {
  [0]=>
  string(3) "add"
  [1]=>
  string(6) "delete"
}

add
delete

Actual result:
--------------
array(2) {
  [0]=>
  string(3) "add"
  [1]=>
  string(6) "delete"
}

Fatal error:  Cannot access undefined property for object with overloaded property access 


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-02-15 10:39 UTC] tony2001@php.net
Please do not submit the same bug more than once. An existing
bug report already describes this very problem. Even if you feel
that your issue is somewhat different, the resolution is likely
to be the same. 

Thank you for your interest in PHP.

See #29378.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 18 15:01:33 2024 UTC