コード例 #1
0
ファイル: magic_methods.php プロジェクト: sneakquie/ClassLib
<?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');
コード例 #2
0
ファイル: class_lib.php プロジェクト: sneakquie/ClassLib
/**
 * Prototype of _private() with defined is_static as true
 * @param string $key
 * @param mixed $value
 * @return array
 */
function _private_static($key, $value = null)
{
    return _private($key, $value, true);
}
コード例 #3
0
ファイル: builder.php プロジェクト: sneakquie/ClassLib
})));
_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');
コード例 #4
0
ファイル: singleton.php プロジェクト: sneakquie/ClassLib
<?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;