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
    [EMAIL PROTECTED]



-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to