#24808 [Bgs]: access to private or protected member throw __get and __set method

From: Date: Mon, 28 Jul 2003 22:49:45 +0000
Subject: #24808 [Bgs]: access to private or protected member throw __get and __set method
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-44941@lists.php.net to get a copy of this message
 ID:               24808
 User updated by:  Bertrand dot Willm at laposte dot net
 Reported By:      Bertrand dot Willm at laposte dot net
 Status:           Bogus
 Bug Type:         Class/Object related
 Operating System: Windows XP
 PHP Version:      5.0.0b1 (beta1)
 New Comment:

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++.


Previous Comments:
------------------------------------------------------------------------

[2003-07-28 13:59:09] 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-25 16:09:31] 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


------------------------------------------------------------------------


-- 
Edit this bug report at http://bugs.php.net/?id=24808&edit=1



Thread (9 messages)

« previous php.bugs (#44941) next »