Objective-PHP

From Electriki
Revision as of 22:26, 14 July 2011 by Yac (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

This is something i wrote some time in 2k9 just for the hell of it

<?php

/**
 * @package OPHPLolwut
 */

Abstract class superObject {
	private $binded=	array();

	function __call($name,$args) {
		if(!isset($this->$name)) {
			if(!isset($this->binded[$name]) || $this->binded[$name] !== true) throw new noMethodError();
			$this->$name= new $name();
		}
		return $this->$name->doIt($this,$args);
	}

/**
 * @param mixed $i input
 * @param array $args arguments
 */
	abstract function doIt($i,$args);

/**
 * @var string $name
 */
	function bind($name) {
		$this->binded[$name]=	true;
	}

	function unbind($name=null) {
		if(isset($this->binded[$name])) {
			$this->binded["name"]=	false;
			unset($this->$name);
		}
	}

	function __construct() {
		$this->bind('hasMethod');
	}
}

class object extends superObject {
	function __construct() {
		parent::__construct();
	}
	function doIt($i,$args) {
		return $this;
	}
}

class superException extends Exception {
	function __construct($message="",$data=null,$code=0) {
		parent::__construct($message,$code);
		$this->data=	$data;
	}
}

class error extends superException {}
class noMethodError extends error {}

class hasMethod extends object {
	function doIt($i,$args) {
		$method=	array_pop($args);
		return method_exists($i,$method);
	}
}


class value extends object {
	function __construct() {
		$this->bind('set');
		$this->bind('get');
		$this->bind('isset_');
	}
}

class set extends object {
	function doIt($i,$args) {
		if(count($args) != 1) throw new superException("",$args);

		if($i instanceOf value) { $i->value=	array_pop($args); return $i; }
		#if($i instanceOf accessor) { $i->value()->set(array_pop($args)); return $i; }
		return new nil();
	}
}

class get extends object {
	function doIt($i,$args) {
		if($i instanceOf value) { if($i->isset_()) return $i->value; }
		if($i instanceOf accessor) { return $i->value()->get(); }
		return new nil();
	}
}

class isset_ {
	function doIt($i,$args) {
		if($i instanceof value) { if(isset($i->value)) return $i->value; }
		return new nil();
	}
}





class accessor extends object {
	function __construct() {
		$this->bind('value');
	}
	function doIt($i,$args) {
		if(isset($args[0])) {
			return $this->value()->set($args[0]);
		}
		return $this->value()->get();
	}
}

#############################################################################


try {

	$a=	new accessor();
	$a->value()->set("abc123");
	echo $a->value()->get();

}catch(SuperException $e) {
	printf($e->getTraceAsString()." ".$e->getFile()."(".$e->getLine().")\nMessage:".$e->getMessage()."\nData:\n".var_export($e->data,1));
}