[ create a new paste ] login | about

Link: http://codepad.org/eShh6R6Z    [ raw code | fork ]

PHP, pasted on May 22:
class callbacks{
	protected $queue=[];

	//fire the queue
	public function __invoke($args){
		for ($i=0;$i<$size;$i++)
	public function add(callable $func){

trait dynamicMethods{
	protected $methods=[];

	final function __call($method, $args){
		if (!isset($this->methods[$method]))
			throw new Exception("Invalid method call: $method. ");
	final function __set($name,$value){
		if (isset($this->methods[$name]))
			throw new Exception("$name is declared as a dynamicMethod. ");
	public function bind($name,callable $func){
		if (!is_string($name))
			throw new Exception("Method name must be a string. ");
		if (isset($this->$name))
			throw new Exception(__CLASS__."->$name is already declared. ");
		if (!isset($this->methods[$name]))
			$this->methods[$name]=new callbacks;

class alfa{
	use dynamicMethods;

$a=new alfa;
	//Creates a new method named 'hola'
	$a->bind('hola', function ($mundo){
		echo "hola $mundo";

	//Calls hola method.

	//Extend the 'hola' method
	$a->bind('hola',function ($mundo){
		echo "adios $mundo";
	$a->hola('mundo cruel.<br/>');

	$a->undeclared();//throws exception.
}catch(Exception $e){
	echo $e->getMessage();
echo $a->hola;//empty response. throws PHP_NOTICE

Create a new paste based on this one