Home  >  Article  >  Backend Development  >  数据库实例化操作的代码优化写法,核心种进行精简了…

数据库实例化操作的代码优化写法,核心种进行精简了…

WBOY
WBOYOriginal
2016-06-13 12:42:16957browse

数据库实例化操作的代码优化写法,核心类进行精简了……

本帖最后由 xjl756425616 于 2013-07-16 16:59:50 编辑
class ActiveRecord<br>
{<br>
    public $table;<br>
    public $data;<br>
    public $obj;    <br>
    public function __construct($table)<br>
    {<br>
    		$this->table = $table;<br>
        $this->data  = array();        <br>
        $this->obj = '';<br>
        $this->connect();<br>
    }<br>
    public function connect()<br>
    {<br>
        $config = array_change_key_case(require("Conf/config.php"));<br>
        if ((!empty($config['db_host'])) && (!empty($config['db_user'])) && (!empty($config['db_name']))) {<br>
            $db_host = $config['db_host'];<br>
            $db_user = $config['db_user'];<br>
            $db_pwd  = $config['db_pwd'];<br>
            $db_name = $config['db_name'];<br>
            $con     = mysql_connect($db_host, $db_user, $db_pwd);<br>
            mysql_select_db($db_name, $con);<br>
            mysql_query("SET NAMES UTF8");<br>
        }<br>
    }<br>
    public function __set($name, $value)<br>
    {<br>
        $this->data[$name] = $value;<br>
        if(is_object($this->obj)) {<br>
        	$this->obj->$name = $value;<br>
      	}<br>
    }<br>
    public function __get($name)<br>
    {<br>
    	if(is_object($this->obj)) {<br>
        	return $this->obj->$name;<br>
      	}<br>
    }<br>
    public function add() <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn