Created
April 13, 2012 15:37
-
-
Save timcrider/2377775 to your computer and use it in GitHub Desktop.
PHP Base Class with simple custom extensions.
This file contains hidden or 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 | |
/** | |
* Base class | |
* | |
* Quick and easy generic object that is more E_NOTICE friendly than normal. | |
* - Variables that do not exist return NULL instead of E_NOTICE errors | |
* - Adding of anonymous functions | |
* | |
* ############################################################################# | |
* # Example 1 - Adding variables and cycling through them | |
* $b = new Base; | |
* $b->a = 1; | |
* $b->b = 2; | |
* $b->c = 3; | |
* | |
* foreach ($b AS $key=>$val) { | |
* print "$key - $val\n"; | |
* } | |
* | |
* # Example 1 output | |
* a - 1 | |
* b - 2 | |
* c - 3 | |
* ############################################################################# | |
* # Example 2 - adding a new method (php5.3+) | |
* | |
* $b = new Base; | |
* $b->addme = function ($a, $b) { return $a+$b; }; | |
* print "Sum of 2+2 =".$b->addme(2,2)."\n"; | |
* print "Sum of 5+10 =".$b->addme(5,10)."\n"; | |
* | |
* # Example 2 output | |
* Sum of 2+2 =4 | |
* Sum of 5+10 =15 | |
* | |
* | |
* @author Timothy M. Crider <[email protected]> | |
*/ | |
class Base extends stdClass { | |
/** | |
* Make this object more E_NOTICE friendly | |
*/ | |
public function __get($var) { | |
return (isset($this->$var)) ? $this->$var : NULL; | |
} | |
/** | |
* Allow attaching methods to the base object | |
*/ | |
public function __call($key, $params) { | |
if (!isset($this->{$key})) { | |
throw new Zend_Exception("Call to undefined method ".get_class()."::{$key}"); | |
} | |
$method = $this->{$key}; | |
return call_user_func_array($method, $params); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment