예제 #1
0
    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)
예제 #2
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);
예제 #3
0
<?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;
예제 #4
0
<?php

include './class_lib.php';
_interface('MyInterface', array(_public('my_function')));
_interface('MySecondInterface', array(_public('my_second_function')));
_class('ParentClass', _implements('MyInterface'), array(_public('my_function', function () {
    return 'Hello, world';
})));
_class('SecondParentClass', _implements('MyInterface', 'MySecondInterface'), array(_public('my_function', function () {
    return 'Hello from ' . _self();
}), _public('my_second_function', function () {
    return 'World, hello!';
})));
_class('Child', _extends('SecondParentClass', 'ParentClass'));
print_r(_call(_new('Child'), 'my_function'));
// output: Hello, world
print_r(_call(_new('Child'), 'my_second_function'));
// output: World, hello!
예제 #5
0
    $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;
    }
    public static function getProduct($id)
    {