you're creating two instances
Code: Select all
$human = new human_t();
$mammal = new mammal_t();
both are (more or less) independent from each other, so there is $human->name and $mammal->name.
After
$mammal->set_name("smith"); $mammal->name is equal to
smith, $human->name is not, because it hasn't been set.
try
Code: Select all
<?php
class mammal_t
{
var $name;
function mammal_t()
{
}
function set_name($name)
{
$this->name = $name;
}
function get_name()
{
return $this->name;
}
}
class human_t extends mammal_t
{
function human_t()
{
}
function get_name()
{
// return the parent's return value
return parent::get_name();
}
}
$humanA = new human_t();
$humanB = new human_t();
$mammalA = new mammal_t();
$mammalB = new mammal_t();
$humanA->set_name('humanA');
$humanB->set_name('humanB');
$mammalA->set_name("mammalA");
$mammalB->set_name("mammalB");
echo $humanA->get_name(), "\n";
echo $humanB->get_name(), "\n";
echo $mammalA->get_name(), "\n";
echo $mammalB->get_name(), "\n";
?>