php中的前后进程功能

时间:2022-03-24 01:57:10

if have a bunch of functions :

如果有一堆功能:

function one () { ... }
function two () { ... }
...
function ten () { ... }

is there a way to execute a pre and post function for each ten functions above WITHOUT MODIFY THEM?

有没有办法为上面的每十个函数执行一个前后函数而没有修改它们?

for instance,

function one () { print '1'; }
function pre () { print 'start'; }
function post () { print 'finish'; }

set_pre_function (one, pre);
set_post_function (one, post);

one();

result :

start
1
finish

4 个解决方案

#1


What about this approach?
You will need to add your functions to the class, and make them private or protected, but you call them directly.

这种方法怎么样?您需要将您的函数添加到类中,并将它们设为私有或受保护,但您可以直接调用它们。

class prePostClass {
    private $preFunc;
    private $postFunc;

    public function set_pre_function($func) {
        $this->preFunc = $func;
    }

    public function set_post_function($func) {
        $this->postFunc = $func;
    }

    public function __call($name, $arguments) {
        if (!is_null($this->preFunc)) {
            $this->preFunc();
        }

        $return = call_user_func_array(__CLASS__ . '->' . $name, $arguments);

        if (!is_null($this->postFunc)) {
            $this->postFunc();
        }

        return $return;
    }
}

#2


try call_user_func :

尝试call_user_func:

   function callfunc($name,$params=false){
         //Call pre 
         //Call real function
         call_user_func($name,$params);
         //and the last, call post
    }

thank @KatrinRaimond :)

谢谢@KatrinRaimond :)

#3


Like this. I haven't used it but its supposed to work.

像这样。我没有使用它,但它应该工作。

function one () 
{ 
  global $pre_one;
  global $post_one;
  $pre_one();
  /* code here */
  $post_one();
  }

func_one() { }
func_two() {}

$pre_one = 'func_one';
$post_one = 'func_two';

#4


You can accomplish this by simply calling your relevant pre and post functions at the beginning and end of the one function. That is, if they need to be their own functions.

您只需在一个函数的开头和结尾调用相关的前置和后置函数即可完成此操作。也就是说,如果他们需要自己的功能。

function pre () { print 'start'; }
function post () { print 'finish'; }
function one () { pre(); print '1'; post(); }
one();

#1


What about this approach?
You will need to add your functions to the class, and make them private or protected, but you call them directly.

这种方法怎么样?您需要将您的函数添加到类中,并将它们设为私有或受保护,但您可以直接调用它们。

class prePostClass {
    private $preFunc;
    private $postFunc;

    public function set_pre_function($func) {
        $this->preFunc = $func;
    }

    public function set_post_function($func) {
        $this->postFunc = $func;
    }

    public function __call($name, $arguments) {
        if (!is_null($this->preFunc)) {
            $this->preFunc();
        }

        $return = call_user_func_array(__CLASS__ . '->' . $name, $arguments);

        if (!is_null($this->postFunc)) {
            $this->postFunc();
        }

        return $return;
    }
}

#2


try call_user_func :

尝试call_user_func:

   function callfunc($name,$params=false){
         //Call pre 
         //Call real function
         call_user_func($name,$params);
         //and the last, call post
    }

thank @KatrinRaimond :)

谢谢@KatrinRaimond :)

#3


Like this. I haven't used it but its supposed to work.

像这样。我没有使用它,但它应该工作。

function one () 
{ 
  global $pre_one;
  global $post_one;
  $pre_one();
  /* code here */
  $post_one();
  }

func_one() { }
func_two() {}

$pre_one = 'func_one';
$post_one = 'func_two';

#4


You can accomplish this by simply calling your relevant pre and post functions at the beginning and end of the one function. That is, if they need to be their own functions.

您只需在一个函数的开头和结尾调用相关的前置和后置函数即可完成此操作。也就是说,如果他们需要自己的功能。

function pre () { print 'start'; }
function post () { print 'finish'; }
function one () { pre(); print '1'; post(); }
one();