Skip to content

Instantly share code, notes, and snippets.

@allada
Created August 19, 2014 02:19
Show Gist options
  • Save allada/b7e880104e3a7eb13a63 to your computer and use it in GitHub Desktop.
Save allada/b7e880104e3a7eb13a63 to your computer and use it in GitHub Desktop.
<?php
class Model{
private static $hookInstalls = array();
public function addHook($hookName, $id, Closure $closure){
// Code to add closure to static::$hookInstalls
}
public $id;
// Updates when record is updated
public function update(){
if(($r = static::runHooks('beforeUpdate', $this)) !== null){
return $r;
}
}
// Run this function to execute any installed hooks pertaining to method/function
public static function runHooks($hookName, $obj){
if(!empty(static::$hookInstalls[$obj->id])){
foreach(static::$hookInstalls[$obj->id] as $closure){
//THIS IS WHERE THE NEW FUNCTION WOULD BE NICE
// but here's what has to happen right now
$newClosure = Closure::bind($closure, $obj, $obj);
if(($r = call_user_func_array($newClosure/*, $args*/)) !== null){
return $r;
}
// new function would look like this:
if(($r = $closure->call($obj/*, $args*/)) !== null){
return $r;
}
}
}
return null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment