All pastes #613260 Raw Edit

k4st

public php v1 · immutable
·published 2007-07-10 21:45 UTC
rendered paste body
<?phperror_reporting(E_ALL);function parse_routes() {		$route = '/action/(int)a/(str)b';	$path = '/action/100/moo';	$vars = array();		$path = preg_split("~/~", $path, -1, PREG_SPLIT_NO_EMPTY);		preg_match_all("~\((?P<type>[a-z]+)\)(?P<var>[^/]*)~i", $route, $matches, PREG_SET_ORDER);		$i = 1;	foreach($matches as $match) {				if(isset($match['type']) && $match['type'] != '') {			$fn = $match['type'] .'val';					$vars[$match['var']] = $fn($path[$i]);		}		$i++;	}		print_r($vars);}parse_routes();exit;function lambda_filter($format, $filter) {	if(!is_array($filter)) {		$filter = array($filter);	}			$args = '';	$letter = 'a';	$sep = '';		foreach($filter as $f) {		$args .= $sep . '$'. $letter++;		$sep = ',';	}			return create_function($args, 'return str_replace('. var_export($filter, TRUE) .', array('. $args .'), "'. str_replace('"', '\"', $format) .'");');}$fn = lambda_filter("/page/%page%/", '%page%');echo $fn(1);echo '<br>';$fn = lambda_filter("/%year%/%month%/%day%/", array('%year%', '%month%', '%day%'));echo $fn('2007', '07', '08');exit;class TestIterable implements IteratorAggregate {	public function getIterator() {		return array('a', 'b', 'c', 'd');	}}$test = new TestIterable;foreach($test as $key) {	echo $key;}exit;class TestCallFunc {	public function __construct($a, $b) {		echo $a .' '. $b;	}}//call_user_func_array(array('TestCallFunc', '__construct'), array('hello', 'sir'));$class = new ReflectionClass('TestCallFunc');$class->newInstance('hello', 'sir');exit;abstract class TestConstructor {		// php-4 style constructor	public function TestConstructor() {		echo 'top';	}		// require a php5 constructor to overwrite php4	// one in all child classes	abstract public function __construct();}class TestConstructorMiddle extends TestConstructor {		// required because of abstract __construct	public function __construct() {		echo 'middle';	}}class TestWithConstructorCall extends TestConstructorMiddle {		// test to see what will happen	public function __construct($test = '') {		parent::__construct();		parent::TestConstructor();	}}new TestWithConstructorCall('');exit;class CallTest {	public function __call($name, array $args = array()) {			}}$test = new CallTest;var_dump(is_callable(array($test, 'moo')));exit;$a = array('a' => 'a', 'b' => 'b');$a[] = array('c' => 'c', 'd' => 'd');print_r($a);exit;class TestLambda {	private $test,			$moo;		public function __construct() {		$this->test = create_function('$this', 'echo $this->moo;');		$this->moo = 'hello world';				//call_user_func($this->test, $this);	}}new TestLambda;exit;class Finder {		static protected $ex;		static public function findAll() {		var_dump(function_exists('parent::findAll'));	}}class Table extends Finder {	}Finder::findAll();Table::findAll();/*// dummy class, needed for parent::class FinderParent {	}class Finder extends FinderParent {    static function findAll()    {			echo '<hr>';    }}class Thing extends Finder {	static $thing;}Finder::findAll();Thing::findAll();*///class GoToOne extends Exception { }//class GoToTwo extends Exception { }/*:(class FooBaz {	public static function __get($key) {		echo 'cool';	}}FooBaz::$test;*/exit;function _create_function($args, $fn) {		static $guid = 0;		$guid++;		eval("function lambda_{$guid}({$args}) {		{$fn}	}");		return 'lambda_'. $guid;}var_dump(_create_function('$a', 'echo "hi";'));var_dump(create_function('$a', 'echo "hi";')); // increments ?lambda_* per page refreshexit;class _ {	const _ = 'test';	public function __construct() {		echo 'hi';	}}function _() {	echo 'hii';}exit;class PersistentObject implements IteratorAggregate {	private $data = array();		public function getIterator() {		return new ArrayIterator($this->data);	}		public function __construct($data = array()) {		if(is_array($data)) {			$this->data = $data;		}	}}class FakeArray implements Serializable {	private $array = array(							'a' => 'b',							'c' => 'd',							'e' => 'f',						);	private $moo = 'hi';		public function serialize() {		$persistent = new PersistentObject($this->array);		return serialize($persistent);	}	public function unserialize($array) {		$array = unserialize($array);				var_dump($array);	}}$moo = new FakeArray(TRUE);$moo = serialize($moo);echo $moo;unserialize($moo);exit;class Whatever {	public $moo;	public function __construct() {		$this->moo = create_function('','echo "lambda";');	}	public function __call($name, $args = array()) {		echo '__call';	}}$whatever = new Whatever;// output: ?lambda_1var_dump($whatever->moo);// output: __call$whatever->moo();//$whatever->moo();//var_dump(class_exists('SplObjectStorage'));exit;/*class ParentObject {	public function parentMethod() {		return $this->a;	}}class TestClassSerializable extends ParentObject implements Serializable {	public function serialize() {		return serialize(get_object_vars($this));	}	// constructor	public function unserialize($serialized) {		$data = unserialize($serialized);				foreach($data as $key => $val) {			$this->$key = $val;		}				return $this;	}	public function __destruct() {		echo 'Serializable: destructor called<br>';	}	final public function someMethod() {		return $this->b;	}	public $a = 'moo';	private $b = 'hii';	protected $c = 'baz';	static public $d = 'lala';}class TestClassMagic extends ParentObject {	public function __sleep() {		return array_keys(get_object_vars($this));	}	// constructor	public function __wakeup() {		return $this;	}	public function __destruct() {		echo 'Magic: destructor called<br>';	}	final public function someMethod() {		return $this->b;	}	public $a = 'moo';	private $b = 'hii';	protected $c = 'baz';	static public $d = 'lala';}*//******************* ROUND TWO *********************//*// using the interface Serializable$test = new TestClassSerializable;$test = serialize($test);$test = unserialize($test);var_dump($test->parentMethod());// using magic methods __sleep and __wakeup$test = new TestClassMagic;$test = serialize($test);$test = unserialize($test);var_dump($test->parentMethod());exit;*//******************* ROUND ONE *********************//*// using the interface Serializable$test = new TestClassSerializable;$test_s = serialize($test);$test_u = unserialize($test_s);var_dump($test_u);var_dump($test_u->someMethod());echo '<hr>';*//*object(TestClassSerializable)#1 (2) {  ["a"]=>  string(3) "moo"  ["b:private"]=>  string(3) "hii"}string(3) "hii"*//*// using magic methods __sleep and __wakeup$test = new TestClassMagic;$test_s = serialize($test);$test_u = unserialize($test_s);var_dump($test_u);var_dump($test_u->someMethod());echo '<hr>';*//*object(TestClassMagic)#2 (2) {  ["a"]=>  string(3) "moo"  ["b:private"]=>  string(3) "hii"}string(3) "hii"*//*class ResultSet {	}// fake db queryfunction db_query($sql) {	// do query here...	echo $sql;}function query_error_handler($error_num, $error_str, $error_file, $error_line) {	if($error_num == 8 && preg_match("~^Undefined variable: update~i", $error_str)) {		return db_query(str_ireplace('Undefined variable: ', '', $error_str));	}	return FALSE;}set_error_handler('query_error_handler');// perform a db query!${"UPDATE mytable SET foo='bar'"};*/exit;$lambda_1;$phpinfo = 'phpinfo';function lambda($args = '', $content = '') {		global $lambda_1;		$lambda_1 = create_function($args, $content);		return 'lambda_1';}echo ${lambda('$a', 'return $a;')}('hello world');exit;class PointlesslyComplicatedAutoload {	static public function autoload($mixed = FALSE) {		static $fns;				if($fns === NULL) {			$mixed = !is_array($mixed) ? array() : $mixed;						$fns = $mixed;						return array('SexyAutoload', 'autoload');		} else {						if(in_array($mixed, $fns) && !class_exists($mixed, FALSE) && file_exists($mixed .'.class.php')) {				require_once $mixed .'.class.php';			}		}		return TRUE;	}	public function __construct() {				spl_autoload_regiser(self::autload(array(												'ValidClass',												'AnotherValidClass',												)));	}}new PointlesslyComplicatedAutoload;//$ret = call_user_func_array(array('Moo', 'Moo'), array('hello'));//Moo::__call('__construct', array('test'));//call_user_func_array(array('Moo', '__call'), array('moooo'));//var_dump($ret->hi);exit;$test = null;var_dump(isset($test['hi']));exit;class CleanUpException extends Exception {		static $instance;		public static function getInstance() {		if(self::$instance === NULL) {			self::$instance = new self;		}		return self::$instance;	}		// public because exception is	public function __construct() {		echo 'construct';	}		public function __destruct() {		echo 'destruct';	}	public function __toString() {				// clean up		$this->__destruct();				return '';	}}try {	throw CleanUpException::getInstance();} catch(Exception $e) {	echo 'inter';}// exceptions destructors are only called if they are caught.. use cases?exit;function test(array $var = array()) {	echo 'hi';}test(0); // errortest(); // goodexit;var_dump(0.0 == 0);exit;echo strtolower(1); exit;var_dump(ctype_digit('1'));exit;/*class Foo implements ArrayAccess {	public $vars = array(		'layers' => new Foo,		'file' => TRUE,		);	public function offsetGet($key) {		$ret = NULL;		if($this->offsetExists($key))			$ret = $this->vars[$key];		return $ret;	}	public function offsetSet($key, $val) {			}	public function offsetUnset($key) {			}	public function offsetExists($key) {		return isset($this->vars[$key]);	}}$foo = new Foo;var_dump($foo['layers']['file']);exit;if(NULL)	echo 'hi';*/exit;$fp = fopen(dirname(__FILE__ .'/test.php'), 'r');var_dump($fp);fclose($fp);var_dump($fp);exit;var_dump((string)NULL);exit;var_dump(parse_url('ftp+ssh://user:pass@host.com:111/path/to/file'));exit;class Test {	public function moo() {		unset($this);	}	public function __destruct() {			}}$test = new Test;$test->moo();var_dump($test);exit;$array = array('list', 'var', 'a', 'b', 'c');print_r(array_splice($array, 2));exit;list($varname, $function) = explode('|', 'hi');var_dump($function);exit;/*class Test {	public $fn = 'phpinfo';}//$test = new Test;//(string)$test();$test = (new Test)->fn;$test();*/exit;//$xml = '<root><sub><sub>text</sub></sub></root>';//print_r(preg_split("~<(/?)([^>]*)>~",$buffer,-1,PREG_SPLIT_DELIM_CAPTURE));//echo base64_encode("X-Captcha: OneLobby http://www.onelobby.com");?>