php – How to check if a function is overridden in derived class?-ThrowExceptions

Exception or error:

I have a base class with a function, and several classes that derive from the base class. Some of these derived classes override the function, some don’t.

Is there a way to check if a particular object, which is known to be one of the derived classes, has overridden that function?

Example:

<?php

class BaseThing
{
    function Bla() { echo "Hello, this is the base class\n"; }
}

class DerivedThing extends BaseThing
{
    function Bla() { echo "Hello, this is a derived class\n"; }
}

class AnotherDerivedThing extends BaseThing
{
    // Does not override Bla()
}

$a = new BaseThing();
$b = new DerivedThing();
$c = new AnotherDerivedThing();

$a->Bla(); // prints base class
$b->Bla(); // prints derived class
$c->Bla(); // prints base class

if (method_exists($b,'Bla')) echo "Method 'Bla' exists in DerivedThing\n";
if (method_exists($c,'Bla')) echo "Method 'Bla' exists in AnotherDerivedThing\n";

?>

I tried using method_exists but apparently it says $c contains the method because it’s derived from a class that does.

Is there a way to check if an object overrides a particular function? E.g. in the example above, can I somehow detect that $b does override the Bla() function but $c does not?

How to solve:

You can use ReflectionClass::getMethod() and compare the methods :

<?php
class BaseThing
{
    function Bla() { echo "Hello, this is the base class\n"; }
}

class DerivedThing extends BaseThing
{
    function Bla() { echo "Hello, this is a derived class\n"; }
}

class AnotherDerivedThing extends BaseThing
{
    // Does not override Bla()
}

$reflectorBase = new ReflectionClass('BaseThing');
$reflectorDerived = new ReflectionClass('DerivedThing');
$reflectorAnotherDerived = new ReflectionClass('AnotherDerivedThing');

if ($reflectorBase->getMethod('Bla') == $reflectorDerived->getMethod('Bla'))
{
    echo "methods are the same in base and derived" . PHP_EOL;
}
else
{
    echo "methods are NOT the same in base and derived" . PHP_EOL;
}

if ($reflectorBase->getMethod('Bla') == $reflectorAnotherDerived->getMethod('Bla'))
{
    echo "methods are the same in base and derived 2" . PHP_EOL;
}
else
{
    echo "methods are NOT the same in base and derived 2" . PHP_EOL;
}

This outputs :

methods are NOT the same in base and derived
methods are the same in base and derived 2

Answer´╝Ü

Again using reflection, (as deceze mentioned) you can get a method and check which class it is declared in…

class BaseThing
{
    function Bla() { echo "Hello, this is the base class\n"; }
}

class DerivedThing extends BaseThing
{
    function Bla() { echo "Hello, this is a derived class\n"; }
}

class AnotherDerivedThing extends BaseThing
{
    // Does not override Bla()
}

$reflectorDerived = new ReflectionClass('DerivedThing');
$method = $reflectorDerived->getMethod("Bla");
echo $method->getDeclaringClass()->name.PHP_EOL;

$reflectorAnotherDerived = new ReflectionClass('AnotherDerivedThing');
$method = $reflectorAnotherDerived->getMethod("Bla");
echo $method->getDeclaringClass()->name;

gives..

DerivedThing
BaseThing

Leave a Reply

Your email address will not be published. Required fields are marked *