|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2005-10-17 16:58 UTC] kai at meder dot info
Description: ------------ chaining of objects using __get() works like a charm, however if using chaining and using __set() at the end of the chain to set a property, php fails reporting: "Fatal error: Cannot access private property A::$p in <file> on line <line>" please keep in mind that i'm using php5.1 (RC1), chaining should work in this version, doesn't it? posted reproduce code from http://www.sitepoint.com/forums/showthread.php?p=2230679#post2230679 Reproduce code: --------------- class A { private $p; function __get($name){ return $this->$name; } function __set($name, $value) { $this->$name = $value; } } class B { private $t; function __get($name){ return $this->$name; } function __set($name, $value) { $this->$name = $value; } } $a = new A; $b = new B; $a->p = $b; $b->t = "foo"; echo $a->p->t; $a->p->t = "bar"; echo $a->p->t; Expected result: ---------------- foobar Actual result: -------------- foo Fatal error: Cannot access private property A::$p in... PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 12:00:02 2025 UTC |
Ilia, Tony, you're right with php5.0 but this has somewhen changed with php 5.1. Taking this short example: <?php class A { private $p; function __set($name, $value) { echo "in __set()\n"; $this->$name = $value; } } $a = new A(); $a->p = true; var_dump($a); ?> With PHP 5.0 this gives: Fatal error: Cannot access private property A::$p" With PHP 5.1 this gives in __set() object(A)#1 (1) { ["p:private"]=> bool(true) } Same goes for __get and __call and private/protected properties. The only problem /seems/ to be the combination of __get and __set as in the $a->p->t = "bar"; line of the original code. So something is broken - either it should work always or never.