|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Thu Oct 30 17:00:01 2025 UTC |
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 emptyDescription: ------------ 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