3

If I have this example :

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();

I want to get array with all called functions and classes, something like:

[
    '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],
    ]
]

Notice: I want solution for whole framework not just bad workaround for this small example.

Prakash Pazhanisamy
  • 997
  • 1
  • 15
  • 25
fico7489
  • 7,931
  • 7
  • 55
  • 89

1 Answers1

0

I would do like this, Make a base class and extends to update your analysis.

<?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);
?>

I know you can update code to get miliseconds and miliseconds_each

Live demo : https://eval.in/879998

Niklesh Raut
  • 34,013
  • 16
  • 75
  • 109
  • thaks for try, but I want elegant solution for whole big system like library or framework, I don't want to touch my classes... – fico7489 Oct 14 '17 at 11:40