Created
August 19, 2014 02:19
-
-
Save allada/b7e880104e3a7eb13a63 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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