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) _call($obj, 'my_function()', 'Not "Hello from private"'); // output: string(13) "my_function()" array(1) { [0]=> string(24) "Not "Hello from private"" } var_dump(_prop($obj, 'my_prop')); // output: NULL var_dump(_prop($obj, 'my_prop', 100)); // output: 100 var_dump(_prop($obj, 'my_prop')); // output: 100 $clone = _clone($obj); var_dump(_prop($clone, 'my_prop')); // output: NULL // Execute destructors // output: int(0) // output: int(0)
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; }