Example #1
0
<?php

include './class_lib.php';
_class('MyClass', array(_private('my_prop', 1), _protected('storage', array()), _public('__construct', function () {
    var_dump(_this('my_prop', 1));
}), _public('__destruct', function () {
    var_dump(_this('my_prop', 0));
}), _private('my_function', function () {
    return 'Hello from private';
}), _private_static('my_static_function', function () {
    return 'Hello from static private';
}), _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');
Example #2
0
/**
 * Prototype of _protected() with defined is_static as true
 * @param string $key
 * @param mixed $value
 * @return array
 */
function _protected_static($key, $value = null)
{
    return _protected($key, $value, true);
}
Example #3
0
<?php

include './class_lib.php';
_class('Pizza', array(_private('_pastry', ''), _private('_sauce', ''), _private('_garniture', ''), _public('getMyPizza', function () {
    return _this('_pastry') . ' ' . _this('_sauce') . ' ' . _this('_garniture') . ' ' . _self();
}), _public('setPastry', function ($pastry) {
    _this('_pastry', $pastry);
}), _public('setSauce', function ($sauce) {
    _this('_sauce', $sauce);
}), _public('setGarniture', function ($garniture) {
    _this('_garniture', $garniture);
})));
_class('BuilderPizza', array(_protected('_pizza'), _public('getPizza', function () {
    return _this('_pizza');
}), _public('createNewPizza', function () {
    _this('_pizza', _new('Pizza'));
}), _public('buildPastry', function () {
}), _public('buildSauce', function () {
}), _public('buildGarniture', function () {
})));
_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');
Example #4
0
        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
 * @link http://habrahabr.ru/post/214285/
 */
class Factory
{
    protected static $products = array();