Last active
December 14, 2015 12:59
Using OOP serialize the chain of php,always used to handle the DB.
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 | |
/** | |
* Created by JetBrains PhpStorm. | |
* User: zhangxy | |
* Date: 13-3-5 | |
* Time: 下午2:31 | |
*/ | |
error_reporting(E_ALL | E_STRICT); | |
class Base_OOP_VAR{ | |
private $_trace_data = array(); | |
protected $_methods = array(); | |
protected $_data; | |
public function __construct($data){ | |
$this->_data = $data; | |
$this->_trace_data['__construct'] = $data; | |
return $this->_data; | |
} | |
public function __toString(){ | |
if(is_int($this->_data) || is_float($this->_data)){ | |
$this->_data = (string)$this->_data; | |
} | |
return $this->_data; | |
} | |
//魔术方法提供链式操作的方法传递到methods数组中 | |
public function __call($name,$args){ | |
$this->valid_func($name); | |
if(!$args) $args = $this->_data; | |
$this->_data = call_user_func($name, $args); | |
$this->_trace_data[$name] = $this->_data; | |
return $this; | |
} | |
//检查方法是否有效 | |
private function valid_func($name){ | |
if(!in_array($name, $this->_methods)){ | |
throw new Exception('invalid method:' . $name . ' in ' . var_dump($this->_methods)); | |
} | |
} | |
//打印trace信息 | |
public function trace(){ | |
echo "<pre>"; | |
var_dump($this->_trace_data); | |
echo "</pre>"; | |
} | |
} | |
class String extends Base_OOP_VAR{ | |
protected $_methods = array( | |
'trim', | |
'strlen', | |
'gettype' | |
); | |
} | |
$a = new String(' Hello world ! '); | |
echo $a->trim()->strlen()->gettype(); | |
$a->trace(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment