Is it possible to tell if a method in the parent class is called explicitly by parent:: as opposed to being called automatically because the sub class doesn't contain the method being called?
well, I'm not sure you can get it easily. anyway, I think you could follow one of this way, if you need a work-around:
example 1:
class Par
{
function printit($which = false)
{
// when you call this method, based on variable it tells how it was called
if ($which) {
echo "called with parent
";
} else {
echo "called with this
";
}
}
}
class Chi extends Par
{
function callParent()
{
parent::printit(TRUE);
}
function callFunction()
{
$this->printit(FALSE);
}
}
$chi = new Chi();
$chi->callParent();
$chi->callFunction();
example 2:
class Par
{
function printit()
{
// get all functions in child class
$child_methods = array_diff(get_class_methods("Chi") , get_class_methods("Par"));
// if the function there is in child class, probably it was called from there
if (in_array(__FUNCTION__, $child_methods)) {
echo "called with child
";
} else {
echo "called with parent
";
}
}
}
class Chi extends Par
{
function callParent()
{
parent::printit();
}
function callFunction()
{
$this->printit();
}
}
$chi = new Chi();
$chi->callParent();
$chi->callFunction();