<?php include './class_lib.php'; _class('MyClass', array(_private('my_prop', 1), _protected('storage', array()), _public('__construct', function () { var_dump(_this('my_prop', 1)); }), _public('__destruct', function () { var_dump(_this('my_prop', 0)); }), _private('my_function', function () { return 'Hello from private'; }), _private_static('my_static_function', function () { return 'Hello from static private'; }), _public('__call', function ($method, $arguments) { var_dump($method); var_dump($arguments); }), _public_static('__callStatic', function ($method, $arguments) { var_dump($method); var_dump($arguments); }), _public('__set', function ($key, $value) { $data = _this('storage'); $data[(string) $key] = $value; _this('storage', $data); return $value; }), _public('__get', function ($key) { $data = _this('storage'); return isset($data[$key]) ? $data[$key] : null; }), _public('__clone', function () { _this('storage', array()); }))); _call('MyClass::my_static_function()', 'Not "Hello from static private"'); // output: string(18) "my_static_function" array(1) { [0]=> string(31) "Not "Hello from static private"" } $obj = _new('MyClass'); // output: int(1)
<?php include './class_lib.php'; _class('Product', array(_private_static('_instance'), _public('a'), _public_static('getInstance', function () { if (!_instanceof(_self('_instance'), _self())) { _self('_instance', _new(_self())); } return _self('_instance'); }), _private('__construct', function () { }), _private('__clone', function () { }))); $firstProduct = _call('Product::getInstance()'); $secondProduct = _call('Product::getInstance()'); _prop($firstProduct, 'a', 1); _prop($secondProduct, 'a', 2); print_r(_prop($firstProduct, 'a')); print_r(_prop($secondProduct, 'a')); /* * Standart PHP example * @link http://habrahabr.ru/post/214285/ */ final class Product { private static $instance; public $a; public static function getInstance() { if (!self::$instance instanceof self) { self::$instance = new self(); } return self::$instance;
<?php include './class_lib.php'; _class('Product', array(_protected_static('_data', array()), _public_static('set', function ($key, $value) { $data = _self('_data'); $data[$key] = $value; _self('_data', $data); }), _public_static('get', function ($key) { $data = _self('_data'); return isset($data[$key]) ? $data[$key] : null; }), _public_static('removeProduct', function ($key) { $data = _self('_data'); if (isset($data[$key]) || array_key_exists($key, $data)) { unset($data[$key]); _self('_data', $data); } }))); _call('Product::set', 'name', 'First product'); print_r(_call('Product::get', 'name')); /* * Standart PHP example * @link http://habrahabr.ru/post/214285/ */ class Product { protected static $data = array(); public static function set($key, $value) { self::$data[$key] = $value; } public static function get($key)
include './class_lib.php'; _class('Factory', array(_protected_static('_products', array()), _public_static('pushProduct', function ($product) { if (!_instanceof($product, 'Product')) { trigger_error('qwerty', E_USER_ERROR); } $data = _self('_products'); $data[_call($product, 'getId')] = $product; _self('_products', $data); }), _public_static('getProduct', function ($id) { $data = _self('_products'); return isset($data[$id]) ? $data[$id] : null; }), _public_static('removeProduct', function ($id) { $data = _self('_products'); if (isset($data[$id]) || array_key_exists($id, $data)) { unset($data[$id]); _self('_products', $data); } }))); _class('Product', array(_protected('_id'), _public('__construct', function ($id) { _this('_id', $id); }), _public('getId', function () { return _this('_id'); }))); _call('Factory::pushProduct', _new('Product', 'first')); _call('Factory::pushProduct', _new('Product', 'second')); print_r(_call(_call('Factory::getProduct', 'first'), 'getId')); print_r(_call(_call('Factory::getProduct', 'second'), 'getId')); /* * Standart PHP example * @link http://habrahabr.ru/post/214285/