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
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
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

Pull Requests

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: Mon Dec 30 18:01:27 2024 UTC