php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #55131 HUITA fix this bug
Submitted: 2011-07-04 14:44 UTC Modified: 2011-07-06 02:26 UTC
From: archon dot saratov at gmail dot com Assigned:
Status: Not a bug Package: *Programming Data Structures
PHP Version: 5.3.6 OS: Windows, FreeBSD
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: archon dot saratov at gmail dot com
New email:
PHP Version: OS:

 

 [2011-07-04 14:44 UTC] archon dot saratov at gmail dot com
Description:
------------
bad working a __get method

Test script:
---------------

<?php
class C1 {
    protected $arr = array( 'e1' => array() );

    public function __get($propertyName) {
        if(isset($this->arr[$propertyName])) return $this->arr[$propertyName];
    }

    public function __set($propertyName, $value) {
        if(isset($this->arr[$propertyName])) $this->arr[$propertyName] = $value;
    }
}

$c1 = new C1();

var_dump($c1->e1);

$c1->e1[] = 1;

var_dump($c1->e1);

Expected result:
----------------
array
  empty

 array
  empty

Actual result:
--------------
array
  empty

( ! ) Notice: Indirect modification of overloaded property C1::$e1 has no effect 
in E:\Server\xampp\htdocs\test.php on line 18

 array
  0 => int 1

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-07-04 14:46 UTC] archon dot saratov at gmail dot com
Description:
------------
bad working a __get method

Test script:
---------------

<?php
class C1 {
    protected $arr = array( 'e1' => array() );

    public function __get($propertyName) {
        if(isset($this->arr[$propertyName])) return $this->arr[$propertyName];
    }

    public function __set($propertyName, $value) {
        if(isset($this->arr[$propertyName])) $this->arr[$propertyName] = $value;
    }
}

$c1 = new C1();

var_dump($c1->e1);

$c1->e1[] = 1;

var_dump($c1->e1);

Expected result:
----------------
array
  empty

 array
  0 => int 1

Actual result:
--------------
array
  empty

( ! ) Notice: Indirect modification of overloaded property C1::$e1 has no effect 
in E:\Server\xampp\htdocs\test.php on line 18

 array
  empty
 [2011-07-04 14:47 UTC] archon dot saratov at gmail dot com
Description:
------------
bad working a __get method

Test script:
---------------

<?php
class C1 {
    protected $arr = array( 'e1' => array() );

    public function __get($propertyName) {
        if(isset($this->arr[$propertyName])) return $this->arr[$propertyName];
    }

    public function __set($propertyName, $value) {
        if(isset($this->arr[$propertyName])) $this->arr[$propertyName] = $value;
    }
}

$c1 = new C1();

var_dump($c1->e1);

$c1->e1[] = 1;

var_dump($c1->e1);

Expected result:
----------------
array
  empty

 array
  0 => int 1

Actual result:
--------------
array
  empty

( ! ) Notice: Indirect modification of overloaded property C1::$e1 has no effect 
in E:\Server\xampp\htdocs\test.php on line 18

 array
  empty
 [2011-07-06 02:26 UTC] scottmac@php.net
-Status: Open +Status: Bogus
 [2011-07-06 02:26 UTC] scottmac@php.net
things returned by __get() are read only.

When you have

$c1->e1[] = 1;

It's really calling __get on e1 and then appending to the array the value 1. The 
latter can't happen since it's read only.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 25 13:01:30 2024 UTC