PHP debug_backtrace in production code to get information about calling method?

后端 未结 7 1001
野趣味
野趣味 2020-11-29 05:43

Is there a compelling reason to not use debug_backtrace for the sole purpose of determining the calling method\'s class, name, and parameter list? Not for debugging purposes

7条回答
  •  隐瞒了意图╮
    2020-11-29 06:28

    It does feel a little dirty, but as has been well documented, opined, and beaten to death elsewhere, PHP isn't a system designed for elegance.

    One highly convoluted reason not to use debug_backtrace for application logic is it's possible some future developer working on PHP could decide "it's just a debug function, performance doesn't matter".

    If you're interested in a "better" way of doing this, you could probably use PHP's magic constants to pass in the calling method and class name, and then use a ReflectionMethod object to extract any other information you need.

    I put better in quotes because, while this would be cleaner and more correct, the overhead of instantiating a Reflection object may be greater than using the debug_backtrace function.

提交回复
热议问题