- debug_backtrace: documentation ( source)
<?php
$wp_version = '6.2.0';
// WP Core code.
class Foo {
public function __call( $name, $arguments ) {
if ( 'new_method_A' === $name
|| 'new_method_B' === $name
|| 'new_method_C' === $name
) {
return $this->$name( ...$arguments );
}
$class = get_class( $this );
$trace = debug_backtrace();
$file = $trace[0]['file'];
$line = $trace[0]['line'];
throw new BadMethodCallException( "Call to undefined method $class::$name() in $file on line $line" );
}
private function new_method_A() {
echo __FUNCTION__, ' called!', PHP_EOL;
}
private function new_method_B() {
echo __FUNCTION__, ' called!', PHP_EOL;
}
public function new_method_C() {
echo __FUNCTION__, ' called!', PHP_EOL;
}
}
// Plugin code.
$obj = new Foo();
try {
$obj->new_method_A();
$obj->new_method_B();
$obj->new_method_C();
} catch(BadMethodCallException $e) {
// Do it some other way to support older WP versions.
} finally {
// Do something with the output of the methods, no matter whether it was created via the code in `try` or the code in `catch`.
}