Yii Framework Forum: debug_backtrace and __callStatic - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

debug_backtrace and __callStatic

#1 User is offline   jimgwhit2 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 27-September 17
  • Location:U.S.A.

Posted 09 October 2017 - 05:49 PM

A lot of modern frameworks have static looking calls to methods in other classes. Even Taylor at laravel says:

Quote

Facades provide a "static" interface to classes that are available in the application's service container.

And

Quote

providing the benefit of a terse, expressive syntax

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.
0

#2 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,184
  • Joined: 17-January 09
  • Location:Russia

Posted 10 October 2017 - 04:20 PM

Quote

what is the best way from the class to return to a friendly error page / view if there is a problem?


Are we talking about Yii error page?
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

#3 User is offline   jimgwhit2 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 49
  • Joined: 27-September 17
  • Location:U.S.A.

Posted 10 October 2017 - 09:30 PM

View Postsamdark, on 10 October 2017 - 04:20 PM, said:

Are we talking about Yii error page?


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.
0

#4 User is offline   samdark 

  • Having fun
  • Yii
  • Group: Yii Dev Team
  • Posts: 5,184
  • Joined: 17-January 09
  • Location:Russia

Posted 14 October 2017 - 07:46 AM

Depends on the handler. Catch the error, output nice message, log error and trace.
Yii 2.0 Development Cookbook

Enjoying Yii? Star us at github

Support me so I can work more on Yii: https://www.patreon.com/samdark
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users