php - Getting the called static class name in a stack trace -
for current project i've built class allows me log information alongside current stack trace. i'm using debug_backtrace()
backtrace, there's "not bug" in whenever inherited static method called, backtrace mentions name of parent class instead of name of actual, called class.
if take example:
<?php class logger { public static function trace() { print_r(debug_backtrace()); } } class animal { public static function create() { logger::trace(); } } class cat extends animal { } class dog extends animal { } cat::create();
this stack trace generated debug_backtrace()
:
array ( [0] => array ( [file] => /t.php [line] => 10 [function] => trace [class] => logger [type] => :: [args] => array ( ) ) [1] => array ( [file] => /t.php [line] => 20 [function] => create [class] => animal [type] => :: [args] => array ( ) ) )
you see, know backtrace animal::create()
executed, don't know whether dog
or cat
, , i'm forced on code find out -- less ideal when i'm looking @ many lines of logs!
is there way build stack trace debug_backtrace()
's invoked class names instead?
note: i'm running php 5.5
maybe try not make static calls, if needed can use service container make dirty job you. service container make not trace correctly, make code testable aswell!!
class logger { public function trace() { print_r(debug_backtrace()); } } class animal { public function create() { $logger = new logger(); $logger->trace(); } } class cat extends animal { } class dog extends animal { } $cat = new cat(); $cat->create();
with no static call, gets me this:
array ( [0] => array ( [file] => /home/daniel/workspace/playground/stack.php [line] => 11 [function] => trace [class] => logger [object] => logger object ( ) [type] => -> [args] => array ( ) ) [1] => array ( [file] => /home/daniel/workspace/playground/stack.php [line] => 22 [function] => create [class] => animal [object] => cat object ( ) [type] => -> [args] => array ( ) ) )
hope helps!
Comments
Post a Comment