diff options
-rw-r--r-- | oo/blank_object.php | 10 | ||||
-rw-r--r-- | oo/magic_methods.php | 93 |
2 files changed, 96 insertions, 7 deletions
diff --git a/oo/blank_object.php b/oo/blank_object.php new file mode 100644 index 0000000..25803d0 --- /dev/null +++ b/oo/blank_object.php @@ -0,0 +1,10 @@ +<?php + +/* stdClass is the default PHP object. stdClass has no properties, methods or parent. + * It does not support magic methods, and implements no interfaces. + * When you cast a scalar or array as Object, you get an instance of stdClass. Bare bones data structure for key-val entries + */ + +$obj = new StdClass(); +$obj->foo = 'bar'; + diff --git a/oo/magic_methods.php b/oo/magic_methods.php index 8d9da79..ecfe70f 100644 --- a/oo/magic_methods.php +++ b/oo/magic_methods.php @@ -1,16 +1,95 @@ <?php -abstract class Base { - protected $arr; +/* overloading in php refers to dynamically create undeclared data members at + * runtime, resulting JS style ability to assign properties to an object + * via -> operator, this is done by use of __set and __get magic methods, also + * it is possible to bypass data members access levels this way + * + * __call is used to resolve call to undefined/protected/private methods, or + * to perform clever yet ugly error handling + * + * http://www.php.net/manual/en/language.oop5.overloading.php + * + */ + +error_reporting(E_ALL | E_STRICT); + +class Base { + protected $data; function __construct() { - echo 'dispatching ctor'; - $this->arr = array(); + $this->data = array(); + } + + /* gets called upon unresolved setter */ + public function __set($key, $val) { + if (!is_a($val, 'Closure')) + print "calling __set('$key','$val')" . PHP_EOL; + $this->data[$key] = $val; + } + + /* gets called upon unresolved getter */ + public function __get($key) { + print "calling __get('$key')" . PHP_EOL; + if (array_key_exists($key, $this->data)) { + return $this->data[$key]; + } + + $trace = debug_backtrace(); + trigger_error( + 'Undefined property via __get(): ' . $key . + ' in ' . $trace[0]['file'] . + ' on line ' . $trace[0]['line'], + E_USER_NOTICE); + return null; + } + + /* http://www.phpied.com/javascript-style-object-literals-in-php/ */ + function __call($name, $args) { + if (is_callable($this->$name)) { + array_unshift($args, $this); + return call_user_func_array($this->$name, $args); + } + + return null; + } + + /** As of PHP 5.1.0 */ + public function __isset($key) + { + echo "Is '$key' set?\n"; + return isset($this->data[$key]); + } + + /** As of PHP 5.1.0 */ + public function __unset($key) + { + echo "Unsetting '$key'\n"; + unset($this->data[$key]); } - abstract protected function foo(); } -class Derived extends Base { +echo "<pre>\n"; + +$obj = new Base(); + +$obj->a = 1; +echo $obj->a . "\n\n"; + +var_dump(isset($obj->a)); +unset($obj->a); +var_dump(isset($obj->a)); +echo "\n"; + + +/* JS like ability to assign a function to a property */ +$obj->foo = function () { + print "hello, world! from method created at runtime!" . PHP_EOL; +}; +$obj->foo(); + + +echo '</pre>' -}
\ No newline at end of file +?> |