How would I go about ensuring that the overridden parent method exists before I call it?
I've tried this:
public function func() { if (function_exists('parent::func')) { return parent::func(); } }
However the function_exists never evaluates to true.
public function func() { if (is_callable('parent::func')) { parent::func(); } }
I use this for calling parent constructor if exists, works fine.
I also use the following as a generic version:
public static function callParentMethod( $object, $class, $methodName, array $args = [] ) { $parentClass = get_parent_class($class); while ($parentClass) { if (method_exists($parentClass, $methodName)) { $parentMethod = new \ReflectionMethod($parentClass, $methodName); return $parentMethod->invokeArgs($object, $args); } $parentClass = get_parent_class($parentClass); } }
use it like this:
callParentMethod($this, __CLASS__, __FUNCTION__, func_get_args());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With