我希望能够在特定函数之前和之后注入一些php代码(一个分析器代码)。函数和文件将被手动插入到表单中,但我希望注入和删除是自动进行的。我使用正则表达式来定位所需的函数调用,但我找不到如何将启用代码放在前面,将禁用代码放在它后面。
发布于 2012-02-13 22:40:30
函数- runkit
您可以使用php runkit来简单地重写该函数。假设您想要替换函数foo(),首先需要用runkit_function_rename()重命名该函数
runkit_function_rename( 'foo', '_foo');而不仅仅是简单地重新定义函数(通过func_get_args()和call_user_func_array()动态处理参数):
function foo() {
// Pre code
$result = call_user_func_array( '_foo', func_get_args());
// Post code
return $result;
}完成后,您可以删除临时函数runkit_function_remove()
runkit_function_remove( 'foo');
// And set up old one back
runkit_function_rename( '_foo', 'foo');如果你真的需要修改函数的代码(内联),"pre callback“和"post callback”是不够的,恐怕我有一个关于你的应用程序设计的坏消息。
方法-简单的包装器
当你需要更新方法(而不是函数)时,你可以用简单的包装器封装整个对象,利用php magic methods,你可能应该实现所有它们,但我将只展示__call(),__set(),__get()和__isset()。
class Wrapper {
// The class that we are about to handle
protected $___data = null;
// Actually the only function that is directly related to Wrapper class
public function __construct( $data){
$this->___data = $data;
}
// By default just call real method
// You may add pre and post callbacks for every function
public function __call( $funcName, $args){
return call_user_func_array( array( $this->___data, $funcName), $args);
}
// Propagate set to deeper level
public function __set( $key, $val){
$result = ($this->___data->{$key} = $val);
if( $result == $this->___data){
return $this;
}
return $result;
}
// Propagate get to deeper level
public function __get( $key){
$result = $this->___data->{$key};
if( $result == $this->___data){
return $this;
}
return $result;
}
// Handles isset
public function __isset( $key){
return isset( $this->___data->{$key});
}
}一旦你有了它,你就可以简单地扩展这个类来对一个方法(比如Bar方法foo()类)进行特殊的处理:
WrapperBar extends Wrapper {
public function foo(){
// Add magick
return call_user_func_array( array( $this->___data, 'foo'), func_get_args());
}
}并将其用作:
$bar = new Bar();
$bar = new WrapperBar( $bar);
$bar->foo( 'One', 'Two', '...');发布于 2012-02-13 22:57:22
我希望能够在一个特定的函数之前和之后注入一些
代码(一个分析器代码)。函数和文件将被手动插入到表单中,但我希望注入和删除是自动进行的。我使用正则表达式来定位所需的函数调用,但我找不到如何将启用代码放在前面,将禁用代码放在它后面。
我想,错误的问题会产生错误的答案。如果您想分析特定的函数,请使用像XDebug这样的分析器,而不是您自己编写的分析器。代码注入来执行分析听起来像是一项可怕的工作,尽管如Vyktor所建议的那样,使用runkit是可能的。
如果你真的想运行你自己的代码,我想最简单的解决方案是这样做:
<?php
$profiler = function( $function ) {
// your profiling code here.
};
$profiler( 'yourfunction' );
yourfunction( );
$profiler( 'yourfunction' );然后,当您完成对应用程序的分析后,您可以简单地为$profiler使用一个不做任何事情的替代函数,这意味着它不会被侵扰。
$profiler = function( $function ) {
return;
};尽管如此,分析仍然遍布您的应用程序。我只会使用现有的工具。
https://stackoverflow.com/questions/9262158
复制相似问题