dynos
November 9th, 2004, 08:16 AM
Look at the below code:
<?php
class Father
{
public function getSalutation()
{
$this->identify();
}
private function identify()
{
print("I am Father.<br>\n");
}
};
class Son extends Father
{
protected function identify()
{
print("I am Son.<br>\n");
}
};
$obj = new Son();
$obj->getSalutation();
?>
If the Son's method "identify" is "private" , the output is "I am Father".
and If it is "public" the output is "I am Father"
That's Ok. Beacuse the Father's "identify" is private, PHP uses static binding for
"$this -> identify()"
But now, it is "protected", the output I think should be "I am Father".
But that's not true, it is "I am Son".
Why?
That's sounds illogic!
PHP 5.0.2 Apache 2.0 Redhat Linux 9.0
<?php
class Father
{
public function getSalutation()
{
$this->identify();
}
private function identify()
{
print("I am Father.<br>\n");
}
};
class Son extends Father
{
protected function identify()
{
print("I am Son.<br>\n");
}
};
$obj = new Son();
$obj->getSalutation();
?>
If the Son's method "identify" is "private" , the output is "I am Father".
and If it is "public" the output is "I am Father"
That's Ok. Beacuse the Father's "identify" is private, PHP uses static binding for
"$this -> identify()"
But now, it is "protected", the output I think should be "I am Father".
But that's not true, it is "I am Son".
Why?
That's sounds illogic!
PHP 5.0.2 Apache 2.0 Redhat Linux 9.0