View Single Post
Old 04-18-2009, 05:00 PM  
quantum-x
Confirmed User
 
quantum-x's Avatar
 
Join Date: Feb 2002
Location: ICQ: 251425 Fr/Au/Ca
Posts: 6,863
Quote:
Originally Posted by KRosh View Post
Why does this work? Why does this show the email address?

#1

Code:
<?php
class person {
    private $email = ?foo?;
    function showEmail() {
        echo $this->email;
    }
}
class user extends person {}
$u = new user();
$u->showEmail();


but this doesn?t?

#2

Code:
<?php
class person {
    private $email = ?foo?;
}
class user extends person {
    function showEmail() {
        echo $this->email;
    }
}
$u = new user();
$u->showEmail();
Also, why does this work

#3

Code:
<?php
class person {
    private $email = ?foo?;
    function showEmail() {
        echo $this->email;
    }}
class user extends person {
    function showEmail() {
        parent::showEmail();
    }
}
$u = new user();
$u->showEmail();

These are all inheritance / overriding questions.
#1 works, because you're extending a class, but not overriding any functions - so the object $user has inherited all the properties and methods of person - basically, you've cloned it.

#2 doesn't work, because you've declared $email in the parent class as private. If you wanted to access that, you'd have to use something like the third method, using an accessor - ie parent::getEmail() - or declare the variable as public (not so good)
quantum-x is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote