php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #24808 access to private or protected member throw __get and __set method
Submitted: 2003-07-25 16:09 UTC Modified: 2004-11-04 15:45 UTC
Votes:4
Avg. Score:5.0 ± 0.0
Reproduced:4 of 4 (100.0%)
Same Version:3 (75.0%)
Same OS:2 (50.0%)
From: Bertrand dot Willm at laposte dot net Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 5* OS: *
Private report: No CVE-ID: None
 [2003-07-25 16:09 UTC] Bertrand dot Willm at laposte dot net
Description:
------------
I want to have access to private or protected member throw __get and __set method to control the access or just to let a read access to this member (and then __set is not used).
To do that I have to choose an other name for this property.
I can't use the same name as the private member.
PHP could test if there is __get or __set method and use them before telling ther is an error.


Reproduce code:
---------------
<?php
class CBaseClass {
   private $var = 'default';
   function __get($name)
   {
      return $this->var;
   }
}

$object = new CBaseClass();
echo $object->var;
?>

Expected result:
----------------
default

Actual result:
--------------
Fatal error: Cannot access private property cbaseclass::$var in c:\sitesweb\www\test.php5 on line 11

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-07-28 13:59 UTC] sfox@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

Try this:

<?php

class CBaseClass {
	private $var = 'default';
	function get($name)
	{
		return $this->var;
	}
}

$object = new CBaseClass();
echo $object->get('name');

?>
 [2003-07-28 17:49 UTC] Bertrand dot Willm at laposte dot net
It is quite nice to tell me to double-check the documentation, but I don't see were it is written that the name given to __get and __set methods can't correspond to private or protected members.
If it is not written, this could be nice to add that information in the documentation.
Perhaps that for a clean programmation this is not a good idea to use the same name for members and properties.
Anyway, after analisys, I found that the current implementation of __get and __set is an unsatsifactory answer to properties. It is far away from what is proposed in C#, Delphi (and of course C++ builder) or visual basic.
To my knowledge, there is no equivalent in java or C++.
 [2003-07-28 18:50 UTC] alan at akbkhome dot com
This has been discussed on zend2-engine, getters and setters on 'defined' vars is a feature that a number of people would like to see..

AFAIK It just needs a voluteer to propose some code..
 [2003-10-16 05:03 UTC] helly@php.net
The behavior is absolut correct. Since the property 'var' is declared private it cannot be accessed. And also since it is a declared property there is no need for the engine to execute __call(). Both __get() and __set() are only there to handle virtual properties (aka not declared ones).

Double check #25815 and #25199
 [2004-05-30 14:43 UTC] stas@php.net
After talking with Andi, I think it's a valid issue and what needs to be done is to make accessors (__get/__set) be called on access to variables that are not visible in current context. This still would not change the fact that acessors are not called on existing variables, but invisible variables in this context would be regarded as good as unexisting.
 [2004-05-31 23:14 UTC] helly@php.net
That seems to be the best solution.
 [2004-07-13 16:16 UTC] de_bruut at hotmail dot com
Quick suggestion:

Please consider adding the same overloading functionality (  __call() ) for private and protected methods.
 [2004-11-04 15:45 UTC] thekid@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip

Reproduce code now returns "default" as expected.

--- Test script --
<?php
  class Foo {
    private $private_member = 'private';
    protected $protected_member = 'protected';
    public $public_member= 'public';
    
    function __get($name) {
      return $this->{$name}.' (via __get)';
    }
  }
  
  $foo= new Foo();
  var_dump($foo->private_member);
  var_dump($foo->protected_member);
  var_dump($foo->public_member);
?>

--- Output --
string(19) "private (via __get)"
string(21) "protected (via __get)"
string(6) "public"

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri May 17 03:01:32 2024 UTC