Click to See Complete Forum and Search --> : A bug, or not?


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

Viper_SB
November 12th, 2004, 12:07 AM
No that's not a bug, protected means that the method can not be changed from any sub classes, this also works the other way and parent classes do not overwrite it either. If you want it to always say "I am Father" then mark the method within the father class as protected.

Are you trying to see what class you are in? or are you just trying to always use one method and have nothing overwrite it?