Skip to content

Instantly share code, notes, and snippets.

@francescoagati
Created September 20, 2012 15:40
Show Gist options
  • Save francescoagati/3756672 to your computer and use it in GitHub Desktop.
Save francescoagati/3756672 to your computer and use it in GitHub Desktop.
play with proxy function with invoke php
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
function node_load($param) {
echo $param;
}
class ProxyFunction {
var $fn_name;
/**
*
* @var Array
*/
var $before = array();
/**
*
* @var Array
*/
var $after = array();
/**
*
* @var Array
*/
var $args = array();
public function __construct($fn_name) {
$this->fn_name = $fn_name;
}
function before($fn) {
$this->before[] = $fn;
return $this;
}
function after($fn) {
$this->after[] = $fn;
return $this;
}
function cycleBefore() {
foreach ($this->before as $fn) {
call_user_func_array($fn, $this->args);
}
}
function cycleAfter() {
foreach ($this->after as $fn) {
call_user_func_array($fn, $this->args);
}
}
function __invoke() {
$this->args = func_get_args();
$this->cycleBefore();
call_user_func_array($this->fn_name, $this->args);
$this->cycleAfter();
}
}
$node_load = (new ProxyFunction("node_load"))
->before(function() {
echo "before";
})
->after(function () {
echo "after";
});
$node_load(22);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment