Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

jlrdw's avatar
Level 75

PHP General 3 (hopefully last)

I have a function I use to get a calling class:

<?php

namespace App\Helpers;

class DBS
{

    public static function __callStatic($method, $params)
    {
        $class = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class'];
        $slh = strrpos($class, "\") + 1;
        $model = "\App\Models\SMVC" . "\" . substr($class, $slh, -10) . "Model";
        $instance = new $model();
        return $instance->$method(...array_values($params));
    }

}

All works good, no problems.

But the question, besides using:

$class = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 2)[1]['class'];

To get the calling class, is there a better way, or is that perfectly alright.

0 likes
4 replies
jlrdw's avatar
Level 75

Will give a try.

I tested, it gives the called class, I am after the calling class, class where call came from. Just seeing if there's a better way. Thank you for replying.

STEREOH's avatar
STEREOH
Best Answer
Level 18

There is nothing to my knowledge besides the backtrace.

I'd pass it as a parameter if it is possible for you.

jlrdw's avatar
Level 75

@STEREOH - Thanks for reply, yeah I kind of figured the backtrace would be it. And I noticed Taylor uses the same at some places to get a calling class or method.

A lot of (regular plain old php) happens behind the scenes in laravel that many are unaware of.

And that's all I am doing, modernizing an older custom framework. I have learned so much from just studying how Taylor does things in the vendor folder.

Please or to participate in this conversation.