Page 1 of 1

php extends problem

Posted: Sun Jul 13, 2008 8:56 pm
by kimipolo
hello everyone
This is my code

Code: Select all

<?php
class people{
    private $names;
    private $age;
    private $test;
    /**/
    function __construct($names__="",$age__=""){
    $this->names=$names__;
    $this->age=$age__;
    }
    /**/
    /**/
    private function __get($w){
        echo '<font color=blue>get</font>';
        return $this->$w;
        }
    /**/
    private function __set($what_names,$what_value){
        echo '<font color=red>set</font>';
    $this->$what_names=$what_value;
    }
    }
class girl extends people{
    private $school;
    
    
    }
/**
$mary=new people('mary_wang','22');
//$mary->names='jerry';
$mary->test='test out put';
echo $mary->test;
echo '<b>'.$mary->names.'</b>';
echo '<b>'.$mary->age.'</b>';
/**/
/**/
$nancy=new girl('nancy_chen','14');
 
$nancy->school='why not out put';
echo $nancy->school;
echo '<b>'.$nancy->names.'</b>';
echo '<b>'.$nancy->age.'<b>';
/**/
?>
my problem is the extends class girl have private school and I create an new class nancy
why do not output 'why not out put' ,this set in the base class

Re: php extends problem

Posted: Mon Jul 14, 2008 1:45 am
by novice4eva
"private" variables cannot be accessed directly by objects. You need to create a function that returns or prints this variable within the class itself. This link must help you out : http://www.hudzilla.org/phpbook/read.php/6_7_2

:drunk:

Re: php extends problem

Posted: Mon Jul 14, 2008 4:23 am
by WebbieDave
You are declaring member variables as private, but then are trying to access them as if they were public.
http://us.php.net/manual/en/language.oo ... bility.php