php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #34893 PHP5.1 overloading, Cannot access private property
Submitted: 2005-10-17 16:58 UTC Modified: 2005-10-20 11:48 UTC
Votes:2
Avg. Score:3.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: kai at meder dot info Assigned: helly (profile)
Status: Closed Package: Scripting Engine problem
PHP Version: 5.1.0RC1 OS: *
Private report: No CVE-ID: None
 [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...

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-10-19 00:43 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

$a->p = $b; is the problem line, you are trying to access a private property from outside the object, this is disallowed.
 [2005-10-19 12:53 UTC] kai at meder dot info
do you even read the bug-reports ?
by trying to set a private-property __get/__set are triggered in php5.1 (yes, it already works!) but there is a serious problem if using __get AND __set together in a chain!
 [2005-10-19 15:47 UTC] iliaa@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

Do you even read documentation? Getters and Setters can only be used to access properties that are not declared in the class.
 [2005-10-19 20:30 UTC] kai at meder dot info
in php5.1 all betas and RC1 __get and __set are ALSO called when trying to access protected/private (invisible) properties.

btw, the manual describes php5, not php5.1 which is in beta/rc-stadium ...

so please forward this *bug* to the overload-developers!
 [2005-10-19 20:52 UTC] tony2001@php.net
You were talking to one of the core developers, so I don't see a reason why you need another one to repeat the same.

 [2005-10-19 21:51 UTC] johannes@php.net
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.  
 [2005-10-20 11:48 UTC] dmitry@php.net
Fixed in CVS HEAD and PHP_5_1
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Nov 21 08:01:29 2024 UTC