*/ ?> <div id="wrapper"> <div id="page-container"> <div id="header"> <div class="container clearfix"> <a href="<?php echo _url('home'); ?> " id="logo">PHPLucidFrame</a> <div id="language-switcher"> <?php $languages = _cfg('languages'); foreach ($languages as $lcode => $lname) { $class = _lang() == $lcode ? 'active' : ''; $url = _self(NULL, $lcode); ?> <a href="<?php echo $url; ?> " class="<?php echo $class; ?> "> <span><?php _image('flags/' . $lcode . '.png', $lname); ?> </span> </a> <?php }
<?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');
<?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!
/** * Test cases for _url() */ public function testForFunctionUnderscoreUrl() { $this->assertEqual(_url(), _self()); $this->assertEqual(_url('home'), rtrim(WEB_ROOT, '/')); $this->assertEqual(_url('blog'), WEB_ROOT . 'blog'); $this->assertEqual(_url('blog', array('this-is-blog-title')), WEB_ROOT . 'blog/this-is-blog-title'); $this->assertEqual(_url('http://example.com'), 'http://example.com'); $this->assertEqual(_url('https://example.com'), 'https://example.com'); $this->assertEqual(_url('https://fb.com/cithu83/posts/12345678'), 'https://fb.com/cithu83/posts/12345678'); }
function _self() { $stack = stack(); if (!isset($stack['class'])) { trigger_error("Using self in wrong context", E_USER_ERROR); } elseif (func_num_args() == 0) { return $stack['class']; } $func_args = func_get_args(); $storage = storage(); if (sizeof($tmp = explode('()', $func_args[0])) > 1) { $function = trim($tmp[0]); $function_l = strtolower($function); unset($func_args[0]); if (!isset($storage['classes'][$stack['class']]['methods'][$function_l])) { if (isset($storage['classes'][$stack['class']]['methods']['__callstatic'])) { _self('__callstatic()', $function, array_values($func_args)); } trigger_error("Call to undefined method {$storage['classes'][$stack['class']]['class']}::" . "{$function}()", E_USER_ERROR); } elseif ($storage['classes'][$stack['class']]['methods'][$function_l]['access'][0] > 1 && isset($storage['classes'][$stack['class']]['methods'][$function_l]['extended']) && (!isset($storage['classes'][$stack['class']]['methods'][$stack['method']]['extended']) || strcasecmp($storage['classes'][$stack['class']]['methods'][$stack['method']]['extended'], $storage['classes'][$stack['class']]['methods'][$function_l]['extended']))) { trigger_error("Call to private method {$storage['classes'][$stack['class']]['methods'][$function_l]['extended']}::" . "{$function}()", E_USER_ERROR); } stack(array('class' => $stack['class'], 'method' => $function_l)); $result = call_user_func_array($storage['classes'][$stack['class']]['methods'][$function_l]['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['classes'][$stack['class']]['properties'][$key]) || !$storage['classes'][$stack['class']]['properties'][$key]['static']) { trigger_error("Access to undeclared static property {$storage['classes'][$stack['class']]['class']}::{$key}", E_USER_ERROR); } elseif (isset($value)) { $storage['classes'][$stack['class']]['properties'][$key]['value'] = $value; storage($storage); } return $storage['classes'][$stack['class']]['properties'][$key]['value']; } }
<?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)
/** * Header redirect to a specific location * @param string $path Routing path such as "foo/bar"; null for the current path * @param array $queryStr Query string as * * array( * $value1, // no key here * 'key1' => $value2, * 'key3' => $value3 or array($value3, $value4) * ) * * @param string $lang The Languague code to be prepended to $path such as "en/foo/bar". * It will be useful for site language switch redirect * @param int $status The HTTP status code - 301 for permanent redirect; * use `_redirect301()` instead; do not provide this for default 302 redirect. * @return void */ function _redirect($path = null, $queryStr = array(), $lang = '', $status = null) { if (stripos($path, 'http') === 0) { if ($status === 301) { header("HTTP/1.1 301 Moved Permanently"); } header('Location: ' . $path); exit; } if ($path == 'self') { $url = _self(null, $lang); } else { $url = route_url($path, $queryStr, $lang); } if ($status === 301) { header("HTTP/1.1 301 Moved Permanently"); } header('Location: ' . $url); exit; }
<?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;
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 * @link http://habrahabr.ru/post/214285/ */