Re: Pb : access control
| From: | Fabrice Le Coz | Date: | Tue, 18 Mar 2003 08:23:28 +0000 |
| Subject: | Re: Pb : access control | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-95200@lists.php.net to get a copy of this message | ||
here a code to test private variables
<?php
// test private variables
class foo {
private $name = "foo";
function __construct() {
$this->name = "foo2";
}
}
class baz extends foo {
function show() {
echo "> name : $this->name \n";
}
}
$test = new baz();
$test->show();
echo " name : $test->name \n";
?>
this code expose the variable foo2 in the baz object (method show) which
normally can't be visible (private variable of the parent class), and expose
the variable in the main context.
Normally a fatal error must be generated when accessing private variables in
the main context (echo " name : $test->name \n";) and in the inheritance
context ($test->show();)
Hope that will help debug PHP5
Fabrice Le Coz
fabrice.lecoz@zedesk.com
----- Original Message -----
From: "Andi Gutmans" <andi@zend.com>
To: "Fabrice Le Coz" <fabrice.lecoz@zedesk.com>; <php-dev@lists.php.net>
Sent: Monday, March 17, 2003 9:00 PM
Subject: Re: [PHP-DEV] Pb : access control
> Can you cut down the script to like 10 lines and say what your result is
> and what you'd expect?
>
> At 05:37 PM 3/17/2003 +0100, Fabrice Le Coz wrote:
> >Hi,
> >
> >I'm playing with PHP5 and have some trouble wtith access control, here's
the
> >code I run with last php5 from snaps.php.net under windows XP :
> >
> ><?php
> >class pere {
> > private $var3;
> > public $var1;
> > protected $var2;
> >
> > function __construct() {
> > $this->var1 = "public";
> > $this->var2 = "protected";
> > $this->var3 = "private";
> > }
> >
> > function show() {
> > echo "pere::show() \n";
> > echo "var1 : $this->var1\n";
> > echo "var2 : $this->var2\n";
> > echo "var3 : $this->var3\n";
> > echo "\n";
> > }
> >}
> >
> >class fils extends pere {
> > protected $var = "fils";
> > private $var4 = "test";
> >
> > function __construct() {
> > parent::__construct();
> > }
> >
> > function show_fils() {
> > echo "fils::show() \n";
> > echo "var1 : $this->var1\n";
> > echo "var2 : $this->var2\n";
> > echo "var3 : $this->var3\n";
> > echo "var : $this->var\n";
> > echo "\n";
> > }
> >}
> >
> >function show_var($obj) {
> > $obj_vars = get_object_vars($obj);
> > foreach ($obj_vars as $name => $value) {
> > if($name != "") echo "$name : $value\n";
> > }
> > echo "\n";
> >}
> >
> >$test1 = new pere();
> >$test1->show();
> >echo "Affichage des variables visibles de test1 :\n";
> >show_var($test1);
> >
> >$test2 = new fils();
> >$test2->show_fils();
> >echo "Affichage des variables visibles de test2 :\n";
> >show_var($test2);
> >$test2->show();
> >echo "var3 : ".$test2->var3."\n";
> >echo "var4 : ".$test2->var4."\n";
> >?>
> >
> >and I've the following result :
> >
> >pere::show()
> >var1 : public
> >var2 : protected
> >var3 : private
> >
> >Affichage des variables visibles de test1 :
> >var1 : public
> >
> >fils::show()
> >var1 : public
> >var2 : protected
> >var3 : private
> >var : fils
> >
> >Affichage des variables visibles de test2 :
> >var1 : public
> >var2 :
> >var3 : private
> >
> >pere::show()
> >var1 : public
> >var2 : protected
> >var3 : private
> >
> >var3 : private
> >
> >Fatal error: Cannot access private property fils::$var4 in
> >D:\www\test\heritier.php on line 59
> >
> >Normally in the instance of "fils" object ($test2), I mustn't see
> >$this->var3 which is private variable of the parent. if I comment the
line
> >'echo "var3 : $this->var3\n";' in the show_fils method,
> >$test2->var3 is
> >empty but do not generate an error !
> >The show_var($test2) function expose a var2 variable which normally must
> >send an Fatal error.
> >
> > Fabrice Le Coz
> > fabrice.lecoz@zedesk.com
> >