是否可以在php中打印所有被调用的类和函数?

时间:2017-10-14 11:16:36

标签: php

如果我有这个例子:

class A {
    public function test(){
        echo 'a';
    }
}
class B {
    public function test(){
        echo 'b;
    }
}
class C {
    public function test(){
        (new A())->test();
    }
}

(new A())->test();
(new B())->test();
(new C())->test();

我想获得所有被调用函数和类的数组,如:

[
    'A' => [
        'function' => 'test',
        'count'    => 2,
        'miliseconds' => 20,
        'miliseconds_each' => [5, 15],
    ],
    'B' => [
        'function' => 'test',
        'count'    => 1,
        'miliseconds' => 25,
        'miliseconds_each' => [25],
    ],
    'C' => [
        'function' => 'test',
        'count'    => 1,
        'miliseconds' => 30,
        'miliseconds_each' => [30],
    ]
]

注意:我想要整个框架的解决方案,而不仅仅是这个小例子的错误解决方法。

1 个答案:

答案 0 :(得分:0)

我会这样做,创建一个基类并扩展以更新您的分析。

<?php
  class Base{
    static $data;
    public static function analysis(){
      return this::data;
    }
    function update($function){
        if(isset(Base::$data[$function])){
          Base::$data[$function] = ['count'=>Base::$data[$function]['count']+1,'function'=>'test'];
        }else{
          Base::$data[$function] = ['count'=>1,'function'=>'test'];
        }
    }
  }
  class A extends Base{
      public function test(){
        $this->update('a');
          echo 'a';
      }
  }
  class B extends Base{
      public function test(){
      $this->update('b');
          echo 'b';
      }
  }
  class C extends Base{
      public function test(){
          (new A())->test();
      }
  }

  (new A())->test();
  (new B())->test();
  (new C())->test();

  print_r(Base::$data);
?>

我知道您可以更新代码以获取milisecondsmiliseconds_each

现场演示:https://eval.in/879998