Ejemplo n.º 1
0
<?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)
Ejemplo n.º 2
0
<?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