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

后端 未结 7 1036
野趣味
野趣味 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:24

    You said in a comment

    The callee's task is to call the same method that called it on a different computer. It's to avoid having a different API function for every possible source, which reduced the amount of code in the system considerably. Is it still bad

    So you want to do the following:

    • Function foo() calls
    • Function reflective(), which does a debug backtrace and requests
    • http://example.com/REST/foo

    Assuming, of course, that you use HTTP requests to fire off the remote call.

    This is a bit of a strange set up. If you are creating the system from scratch then I'd suggest trying to work around it. If you're shoehorning it into a legacy system then I suppose I understand.

    I'd suggest that you always be more explicit when you can. Your use of magic stack introspection might save you a little bit of coding, but to another developer your code will be completely baffling. If'dsuggest that you pass the class and function name to the function that was previously doing the reflection. Then there is no ambiguity about what is happening.

    • Function foo() calls
    • Function reflective(__CLASS__, __FUNCTION__), which requests
    • http://example.com/REST/foo

提交回复
热议问题