示例#1
0
}), _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)
_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'));
示例#2
0
function _this()
{
    $stack = stack();
    if (!isset($stack['object'])) {
        trigger_error("Using \$this when not in object context", E_USER_ERROR);
    } elseif (func_num_args() == 0) {
        return $stack['object'];
    }
    $func_args = func_get_args();
    $storage = storage();
    if (sizeof($tmp = explode('()', $func_args[0])) > 1) {
        $key = strtolower(trim((string) $tmp[0]));
        if (!isset($storage['classes'][$stack['class']]['methods'][$key])) {
            if (isset($storage['classes'][$stack['class']]['methods']['__call'])) {
                return _call($stack['object'], '__call', $key, array_slice(func_get_args(), 2));
            }
            trigger_error("Call to undefined method {$storage['classes'][$stack['class']]['class']}::" . "{$storage['classes'][$stack['class']]['methods'][$key]['name']}()", E_USER_ERROR);
        } elseif ($storage['classes'][$stack['class']]['methods'][$key]['access'][0] > 1 && isset($storage['classes'][$stack['class']]['methods'][$key]['extended']) && (!isset($storage['classes'][$stack['class']]['methods'][$stack['method']]['extended']) || strcasecmp($storage['classes'][$stack['class']]['methods'][$key]['extended'], $storage['classes'][$stack['class']]['methods'][$stack['method']]['extended']))) {
            trigger_error("Call to private method {$storage['classes'][$stack['class']]['methods'][$key]['extended']}::" . "{$storage['classes'][$stack['class']]['methods'][$key]['name']}()", E_USER_ERROR);
        }
        unset($func_args[0]);
        stack(array('class' => $stack['class'], 'method' => $key, 'object' => $stack['object']));
        $result = call_user_func_array($storage['classes'][$stack['class']]['methods'][$key]['value'], array_values($func_args));
        stack(true);
        return $result;
    } else {
        $key = strtolower(trim((string) $func_args[0]));
        sizeof($func_args) > 1 && ($value = $func_args[1]);
        if (!isset($storage['objects'][$stack['object']]['properties'][$key])) {
            if (isset($storage['classes'][$stack['class']]['methods'][isset($value) ? '__set' : '__get'])) {
                return _this(isset($value) ? '__set()' : '__get()', $key, isset($value) ? $value : null);
            }
            trigger_error("Trying to access undefined property {$storage['classes'][$stack['class']]['class']}::{$key}", E_USER_ERROR);
        } elseif (isset($value)) {
            $storage['objects'][$stack['object']]['properties'][$key]['value'] = $value;
            storage($storage);
        }
        return $storage['objects'][$stack['object']]['properties'][$key]['value'];
    }
}
示例#3
0
_class('BuilderPizzaHawaii', _extends('BuilderPizza'), array(_public('buildPastry', function () {
    _call(_this('_pizza'), 'setPastry', 'normal');
}), _public('buildSauce', function () {
    _call(_this('_pizza'), 'setSauce', 'soft');
}), _public('buildGarniture', function () {
    _call(_this('_pizza'), 'setGarniture', 'jambon+ananas');
})));
_class('BuilderPizzaSpicy', _extends('BuilderPizza'), array(_public('buildPastry', function () {
    _call(_this('_pizza'), 'setPastry', 'puff');
}), _public('buildSauce', function () {
    _call(_this('_pizza'), 'setSauce', 'hot');
}), _public('buildGarniture', function () {
    _call(_this('_pizza'), 'setGarniture', 'pepperoni+salami');
})));
_class('PizzaBuilder', array(_private('_builderPizza'), _public('setBuilderPizza', function ($mp) {
    _this('_builderPizza', $mp);
}), _public('getPizza', function () {
    return _call(_this('_builderPizza'), 'getPizza');
}), _public('constructPizza', function () {
    _call(_this('_builderPizza'), 'createNewPizza');
    _call(_this('_builderPizza'), 'buildPastry');
    _call(_this('_builderPizza'), 'buildSauce');
    _call(_this('_builderPizza'), 'buildGarniture');
})));
$pizzaBuilder = _new('PizzaBuilder');
$builderPizzaHawaii = _new('BuilderPizzaHawaii');
$builderPizzaPiquante = _new('BuilderPizzaSpicy');
_call($pizzaBuilder, 'setBuilderPizza', $builderPizzaHawaii);
_call($pizzaBuilder, 'constructPizza');
$pizza = _call($pizzaBuilder, 'getPizza');
ClassLib\var_dump($pizza);
示例#4
0
    $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/
 */
class Factory
{
    protected static $products = array();
    public static function pushProduct(Product $product)
    {
        self::$products[$product->getId()] = $product;