日期:2014-05-17  浏览次数:20389 次

数据库实例化操作的代码优化写法,核心类进行精简了……
本帖最后由 xjl756425616 于 2013-07-16 16:59:50 编辑
class ActiveRecord
{
    public $table;
    public $data;
    public $obj;    
    public function __construct($table)
    {
     $this->table = $table;
        $this->data  = array();        
        $this->obj = '';
        $this->connect();
    }
    public function connect()
    {
        $config = array_change_key_case(require("Conf/config.php"));
        if ((!empty($config['db_host'])) && (!empty($config['db_user'])) && (!empty($config['db_name']))) {
            $db_host = $config['db_host'];
            $db_user = $config['db_user'];
            $db_pwd  = $config['db_pwd'];
            $db_name = $config['db_name'];
            $con     = mysql_connect($db_host, $db_user, $db_pwd);
            mysql_select_db($db_name, $con);
            mysql_query("SET NAMES UTF8");
        }
    }
    public function __set($name, $value)
    {
        $this->data[$name] = $value;
        if(is_object($this->obj)) {
         $this->obj->$name = $value;
       }
    }
    public function __get($name)
    {
     if(is_object($this->obj)) {
         return $this->obj->$name;
       }
    }
    public function add()