php extends problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kimipolo
Forum Newbie
Posts: 2
Joined: Sun Jul 13, 2008 8:44 pm

php extends problem

Post 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
User avatar
novice4eva
Forum Contributor
Posts: 327
Joined: Thu Mar 29, 2007 3:48 am
Location: Nepal

Re: php extends problem

Post 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:
WebbieDave
Forum Contributor
Posts: 213
Joined: Sun Jul 15, 2007 7:07 am

Re: php extends problem

Post 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
Post Reply