Quote:
Originally Posted by KRosh
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)