zoukankan      html  css  js  c++  java
  • 用Exception获取 函数/方法 调用过程

    通常我们在某些拿不准的地方会抛出一个异常来表示,调用者可以输出该异常来追踪错误来源。

    其实也可以使用Exception的实例来获取调用过程,这在分析一个函数/方法的调用情况比较方便

    class A {
        public static function hello() {
            b();
        }
    }
    
    function b() {
        a();
    }
    
    function a() {
        $e = new Exception("hello");
        //echo "file: " . $e->getFile() . PHP_EOL;
        //echo "line: " . $e->getLine() . PHP_EOL;
        //echo "message: " . $e->getMessage() . PHP_EOL;
        //echo "trace: " . $e->getTrace() . PHP_EOL;
        //echo "traceString: " . $e->getTraceAsString() . PHP_EOL;
    
        var_dump($e->getTrace());
    }
    
    A::hello();

    将会获得

    array(3) {
      [0]=>
      array(4) {
        ["file"]=>
        string(40) "A.php"
        ["line"]=>
        int(11)
        ["function"]=>
        string(1) "a"
        ["args"]=>
        array(0) {
        }
      }
      [1]=>
      array(4) {
        ["file"]=>
        string(40) "A.php"
        ["line"]=>
        int(6)
        ["function"]=>
        string(1) "b"
        ["args"]=>
        array(0) {
        }
      }
      [2]=>
      array(6) {
        ["file"]=>
        string(40) "A.php"
        ["line"]=>
        int(25)
        ["function"]=>
        string(5) "hello"
        ["class"]=>
        string(1) "A"
        ["type"]=>
        string(2) "::"
        ["args"]=>
        array(0) {
        }
      }
    }
  • 相关阅读:
    iOS AppStore个人开发者账号申请
    一个工程多个Target
    React Native环境搭建(iOS、Mac)
    vuex的简单使用
    在vue中使用sass
    一个javascript继承和使用的例子
    在vue中使用Element-UI
    CSS3 Flex布局和Grid布局
    (...)ES6三点扩展运算符
    h5 video切换到横屏全屏
  • 原文地址:https://www.cnblogs.com/mtima/p/3578706.html
Copyright © 2011-2022 走看看