drtoclr046994545 2018-06-07 18:42
浏览 46

PHP:当继承的类调用父函数时记录或跟踪

I'm in the process of trying to track outdated or unused functions inside an object class. One idea I had was to create a new class that inherits that original class, and then "track" when that parent's functions are called. When detected, I will migrate the parent's function into the child until eventually only the necessary/needed functions exist.

Is there code that does this automatically within PHP? Here is in example.

class OldUser {
    function getFullName() {
        return "{$this->firstName} {$this->lastName}";
    }
}

class User extends OldUser {

}

-----

$user = new User;
echo $user->getFulLName();

Then in a log somewhere I note: "{timestamp} function getFullName() called"

Obviously I could add these logs manually, but if there is an existing way for PHP to do it I'd love to default to that methodology.

Alternatively, if there is a better way to do this I am open to suggestions.

Thanks!

  • 写回答

3条回答 默认 最新

  • dream989898 2018-06-07 18:55
    关注

    All you need to do from there is implement all of the public methods of OldUser and have those calls trigger the logging and then call the parent function. eg:

    class User extends OldUser {
        protected function log($method, $backtrace) {
            // ...
        }
        public function getFullName() {
            $this->log(__METHOD__, debug_backtrace());
            return parent::getFullName();
        }
    }
    

    debug_backtrace() will allow you to track down where in the code that the function was called.

    评论

报告相同问题?

悬赏问题

  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀