note 60440 added to language.oop5.visibility
| From: | cyrille dot berliat_ at t_free dot fr | Date: | Fri, 06 Jan 2006 23:57:37 +0000 |
| Subject: | note 60440 added to language.oop5.visibility | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-101330@lists.php.net to get a copy of this message | ||
<?php
class test
{
function __construct ()
{
$this->privvar = 'Hello world';
$this->provar = 'Protected, isn\'t it ?';
$this->otherone = 'Hello, I\'m private... hum... I think!';
}
private $privvar;
protected $provar;
private $otherone;
}
function GetProtectedOrPrivateProperty ( & $object, $Property )
{
$arrayObj = (array) $object; // cast to array, in order to access all properties...
$private = chr(0).get_class ( $object ).chr(0).$Property; //build "private index"
$protected = chr(0).'*'.chr(0).$Property; //build "protected index"
if ( array_key_exists($private,$arrayObj) ) // does it exist in private scope ?
{
return $arrayObj [ $private ];
}
if ( array_key_exists($protected,$arrayObj) ) // does it exist in protected scope ?
{
return $arrayObj [ $protected ];
}
return NULL;
}
$test = new test(); // let's test...
$var = & GetProtectedOrPrivateProperty( $test , 'otherone'); // get the
private/protected Property by reference
echo $var.'<br />'; // print it out
$var = 'What ? Am I discovered ?'; // wanna change it ... will it work ?
echo GetProtectedOrPrivateProperty( $test , 'otherone'); // Had it changed ?
?>
And magic happens...
Hello, I'm private... hum... I think!
What ? Am I discovered ?
I created this code to bypass a strange fact...
<?php
class childTest extends test
{
function __construct( test $parentobject )
{
echo $parentobject->provar; // provar is a parent's protected property so it may be
accessible.
}
}
$child = new childTest ($test );
?>
Fatal error: Cannot access protected property test::$provar in
W:\www\exetron\v2\testProtectedPrivate.php on line 61
bye bye...
PHP 5.0.4
----
Manual Page -- http://www.php.net/manual/en/language.oop5.visibility.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+60440
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60440&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60440&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60440&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60440&report=yes&reason=useless
Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60440&report=yes&reason=non-english
Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60440&report=yes&reason=already+in+docs
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60440&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+60440&report=yes
Search -- http://master.php.net/manage/user-notes.php