function init($args) { global $Buffer; list($subject, $encoding, $offset) = array_pad($args, 3, null); $type = gettype($subject); if ($type === 'integer' || $type === 'double') { $this->raw = str_repeat("", (int) $subject); } else { if ($type === 'string') { $encoding = $encoding === null ? 'utf8' : to_string($encoding); if ($encoding === 'hex') { $this->raw = hex2bin($subject); } else { if ($encoding === 'base64') { $this->raw = base64_decode($subject); } else { $this->raw = $subject; } } } else { if (_instanceof($subject, $Buffer)) { $this->raw = $subject->raw; } else { if ($subject instanceof Arr) { $this->raw = $util['arrToRaw']($subject); } else { throw new Ex(Error::create('Invalid parameters to construct Buffer')); } } } } $len = strlen($this->raw); //save an integer copy of length for performance $this->length = $len; $this->set('length', (double) $len); }
/** * Check is object is an instance of class * @param string $object * @param string $class * @return boolean */ function _instanceof($object, $class) { $object = trim((string) $object); $class = trim((string) $class); // Object name isn't allowed if (empty($object)) { return false; } elseif (empty($class)) { // Class name isn't allowed trigger_error("Class name cannot be empty", E_USER_ERROR); } $class_l = strtolower($class); $storage = storage(); // Class doesn't exists if (!isset($storage['classes'][$class_l])) { trigger_error("Class {$class} not found", E_USER_ERROR); } elseif (!isset($storage['objects'][$object])) { // Object doesn't exists return false; } elseif ($class_l === $storage['objects'][$object]['class_l']) { return true; } elseif (isset($storage['classes'][$class_l]['extends'][0])) { // Foreach class parents check if is instance foreach ($storage['classes'][$class_l]['extends'] as $value) { if (_instanceof($object, $value)) { return true; } } } return false; }
<?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;
}); $Animal->get('prototype')->set('speak', new Func(function () { return 'hi'; })); $Dog = new Func(function () { }); $Dog->set('prototype', $Object->callMethod('create', $Animal->get('prototype'))); $dog = $Dog->construct(); Test::assert('has method', $dog->get('speak') instanceof Func); Test::assert('method call', $dog->callMethod('speak') === 'hi'); Test::assert('proto inherit', _instanceof($dog, $Dog)); Test::assert('proto inherit parent', _instanceof($dog, $Animal)); Test::assert('proto inherit top', _instanceof($dog, $Object)); $Thing = new Func(function () { }); Test::assert('proto not instance foreign', !_instanceof($dog, $Thing)); $Dog->get('prototype')->set('speak', new Func(function () { return 'woof'; })); Test::assert('method override', $dog->callMethod('speak') === 'woof'); $animal = $Animal->construct(); Test::assert('method still on parent', $animal->callMethod('speak') === 'hi'); }); Test::suite('Object.keys', function () use($Object, $Array, $JSON) { $obj = $Object->construct(); $obj->set('a', 1.0); $obj->set('b', 2.0); $keys = $Object->callMethod('keys', $obj); Test::assert('basic keys', $keys->callMethod('toString') === 'a,b'); $arr = new Arr(1.0, 2.0); $keys = $Object->callMethod('keys', $arr);
<?php 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