如果我有这个例子:
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],
]
]
注意:我想要整个框架的解决方案,而不仅仅是这个小例子的错误解决方法。
答案 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);
?>
我知道您可以更新代码以获取miliseconds
和miliseconds_each