debug_backtrace and __callStatic


(Kpsklab) #1

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.


(Alexander Makarov) #2

Are we talking about Yii error page?


(Kpsklab) #3

No the older Simple mvc framework v 2.2, I have heavily modified, made php 7 compatible, etc. This ver is no longer supported.

I am not good on error handling, how would one, if there is an error just redirect to another page from this class?

This older framework uses normal redirect. I just don’t want a user to view error, rather a friendly page.

Thanks for the reply.


(Alexander Makarov) #4

Depends on the handler. Catch the error, output nice message, log error and trace.