A lot of modern frameworks have static looking calls to methods in other classes. Even Taylor at laravel says:
And
In an older custom framework I use, I am making a "static" interface to some non static methods. Namely via this class:
<?php
namespace app\helpers;
class DBS
{
public static function __callStatic($method, $params)
{
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2);
if (isset($trace[1]['class'])) {
$class = $trace[1]['class'];
$slh = strrpos($class, "\\") + 1;
} else {
return null;
}
$model = "\app\models\SMVC" . "\\" . substr($class, $slh, -10) . "Model";
$instance = new $model();
return call_user_func_array(array($instance, $method), $params);
}
}
Basically I get the calling class i.e.,
app\controllers\DogController; as an example
then using strrpos and substr I build:
\app\models\SMVC\DogModel as example
Several classes use this, thus dynamic, but all same beginning namespace.
So far it works, and if problem I have return null.
My question, what is the best way from the class to return to a friendly error page / view if there is a problem?
And / or is there a better way to get the calling class when using __callStatic.
Thanks, any tips appreciated.