Last active
December 11, 2015 11:28
-
-
Save moolex/4593352 to your computer and use it in GitHub Desktop.
OOP连贯操作管理
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 实际业务操作 | |
*/ | |
class business | |
{ | |
/** | |
* 操作入口 | |
*/ | |
public static function api() | |
{ | |
return LinkupManager::create('business', LinkupManager::CALLBACK_STATIC); | |
} | |
/** | |
* 定义此方法后如果未找到回调函数时会执行这个 | |
* @param type $process | |
* @param type $args | |
*/ | |
public static function __linkup_callback($process, $args) | |
{ | |
return implode('_', $process).' = '.var_export($args, true); | |
} | |
/** | |
* 获取客户端浏览器版本 | |
*/ | |
public static function client_browser_version_get($args = array()) | |
{ | |
return 'Client Browser Version is ['.var_export($args, true).']'; | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* 连贯操作管理 | |
*/ | |
class LinkupManager | |
{ | |
/** | |
* 对象方式回调 | |
*/ | |
const CALLBACK_OBJECT = 0; | |
/** | |
* 静态方式回调 | |
*/ | |
const CALLBACK_STATIC = 1; | |
/** | |
* 创建一个连续操作对象 | |
*/ | |
public static function create($callback, $type = self::CALLBACK_STATIC) | |
{ | |
return new self($callback, $type == self::CALLBACK_STATIC ? self::CALLBACK_STATIC : self::CALLBACK_OBJECT); | |
} | |
/** | |
* 释放操作对象并返回操作结果 | |
* @param type $object | |
* @param type $result | |
*/ | |
private static function destory($object, $result = false) | |
{ | |
unset($object); | |
return $result; | |
} | |
/** | |
* 返回错误信息(方法未找到) | |
* @param type $object | |
*/ | |
private static function error($object) | |
{ | |
self::destory($object); | |
// return false or throw exception ? | |
return false; | |
} | |
/** | |
* 回调对象 | |
* @var type | |
*/ | |
private $callback = null; | |
/** | |
* 回调类型 | |
* @var type | |
*/ | |
private $callback_type = null; | |
/** | |
* 过程函数 | |
* @var type | |
*/ | |
private $process_function = array(); | |
/** | |
* 过程参数 | |
* @var type | |
*/ | |
private $process_arguments = array(); | |
/** | |
* 初始化 | |
* @param type $callback | |
* @param type $type | |
*/ | |
public function __construct($callback, $type) | |
{ | |
$this->callback = $callback; | |
$this->callback_type = $type; | |
} | |
/** | |
* 操作开始 | |
*/ | |
public function cmd() | |
{ | |
// clear | |
$this->process_function = array(); | |
$this->process_arguments = array(); | |
return $this; | |
} | |
/** | |
* 操作数据收集 | |
* @param type $name | |
* @param type $arguments | |
*/ | |
public function __call($name, $arguments) | |
{ | |
$this->process_function[] = $name; | |
$arguments && $this->process_arguments[$name] = count($arguments) > 1 ? $arguments : current($arguments); | |
return $this; | |
} | |
/** | |
* 操作结束 | |
*/ | |
public function commit() | |
{ | |
$process = implode('_', $this->process_function); | |
$trys = array($process, '__linkup_callback'); | |
$object = null; | |
$args = array(); | |
foreach ($trys as $function) | |
{ | |
if (method_exists($this->callback, $function)) | |
{ | |
$this->object_args_init($object, $args, $function); | |
break; | |
} | |
} | |
if ($object) | |
{ | |
return self::destory($this, call_user_func_array($object, $args)); | |
} | |
else | |
{ | |
return self::error($this); | |
} | |
} | |
/** | |
* 设置回调格式 | |
* @param type $object | |
* @param type $args | |
* @param type $function | |
*/ | |
private function object_args_init(&$object, &$args, $function) | |
{ | |
if ($this->callback_type == self::CALLBACK_STATIC) | |
{ | |
$object = $this->callback.'::'.$function; | |
} | |
if ($this->callback_type == self::CALLBACK_OBJECT) | |
{ | |
$object = array($this->callback, $function); | |
} | |
if ($function == '__linkup_callback') | |
{ | |
$args = array($this->process_function, $this->process_arguments); | |
} | |
else | |
{ | |
$args = array($this->process_arguments); | |
} | |
} | |
} | |
?> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'oop.linkup.php'; | |
require 'business.php'; | |
// 效率测试 | |
$imax = 1000; | |
// 使用连贯操作 | |
$ts = microtime(true); | |
$ms = memory_get_usage(); | |
for ($i = 1; $i < $imax; $i ++) | |
{ | |
$result = business::api()->cmd()->client()->browser('chrome')->version(7)->get(1)->commit(); | |
} | |
$tf = microtime(true); | |
$mf = memory_get_usage(); | |
test_result('Use Linkup', $ts, $tf, $ms, $mf); | |
// 使用原生操作 | |
$ts = microtime(true); | |
$ms = memory_get_usage(); | |
for ($i = 1; $i < $imax; $i ++) | |
{ | |
$result = business::client_browser_version_get(array('browser' => 'chrome', 'version' => 7, 'get' => 1)); | |
} | |
$tf = microtime(true); | |
$mf = memory_get_usage(); | |
test_result('Use Original', $ts, $tf, $ms, $mf); | |
/** | |
* Test Output | |
* @param type $msg | |
* @param type $ts | |
* @param type $tf | |
* @param type $ms | |
* @param type $mf | |
*/ | |
function test_result($msg, $ts, $tf, $ms, $mf) | |
{ | |
$tu = $tf - $ts; | |
$mu = $mf - $ms; | |
echo '<pre>'; | |
echo 'Test : '.$msg.' : [Time:'.$tu.'/Memory:'.$mu.']'; | |
echo '</pre>'; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment