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();
This works because the email property is within scope of the method accessing it.
Quote:
Originally Posted by KRosh
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();
This doesn't work, because email is out of scope. If you set it to "protected" then extending classes will be able it inherit the email property.
Quote:
Originally Posted by KRosh
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();
This works because, again the property is within scope of the method calling it.
Also, you shouldn't be defining properties like that. That should be done in the constructor. The only time you'd do that are with class constants.
http://www.php.net/construct