2013-06-22 39 views
1

我是一个新手,哎呀..类VS功能空中接力

我要通过这本书Php 5 social networking这本书给了 一步步的说明使用哎呀从头创建一个社交网站。

的第一步是在书中给出的是创建下面给出的任务一堆核心类:

  • 数据库访问

  • 模板管理

  • 认证

  • 电子邮件发送

  • URL处理

然后,他们使用了一个注册表类来包装这些对象。

这是数据库访问类的代码:

/** 
* Database management/access class: basic abstraction 
* 
* @author Michael Peacock 
* @version 1.0 
*   
*/ 
class Mysqldb { 
    /** 
    * Allows multiple database connections 
    * each connection is stored as an element in the array, and the 
    * active connection is maintained in a variable (see below) 
    */ 
    private $connections = array(); 
    /** 
    * Tells the DB object which connection to use 
    * setActiveConnection($id) allows us to change this 
    */ 
    private $activeConnection = 0; 
    /** 
    * Queries which have been executed and the results cached for 
    * later, primarily for use within the template engine 
    */ 
    private $queryCache = array(); 
    /** 
    * Data which has been prepared and then cached for later usage, 
    * primarily within the template engine 
    */ 
    private $dataCache = array(); 
    /** 
    * Number of queries made during execution process 
    */ 
    private $queryCounter = 0; 

    /** 
    * Record of the last query 
    */ 
    private $last; 
    /** 
    * Reference to the registry object 
    */ 
    private $registry; 

    /** 
    * Construct our database object 
    */ 
    public function __construct(Registry $registry) { 
     $this->registry = $registry; 
    } 

    /** 
    * Create a new database connection 
    * @param String database hostname 
    * @param String database username 
    * @param String database password 
    * @param String database we are using 
    * @return int the id of the new connection 
    *   
    */ 
    public function newConnection($host, $user, $password, $database) { 
     $this->connections[] = new mysqli($host, $user, $password, $database); 
     $connection_id = count($this->connections) - 1; 
     if (mysqli_connect_errno()) { 
      trigger_error('Error connecting to host. ' . $this->connections[$connection_id]->error, E_USER_ERROR); 
     } 
     return $connection_id; 
    } 

    /** 
    * Change which database connection is actively used for the next 
    * operation 
    * @param int the new connection id 
    * @return void 
    * 
    */ 
    public function setActiveConnection(int $new) { 
     $this->activeConnection = $new; 
    } 

    /** 
    * Execute a query string 
    * @param String the query 
    * @return void 
    * 
    */ 
    public function executeQuery($queryStr) { 
     if (! $result = $this->connections[$this->activeConnection]->query($queryStr)) 

     { 
      trigger_error('Error executing query: ' . $queryStr . ' - 
' . $this->connections[$this->activeConnection]->error, E_USER_ERROR); 
     } else 

     { 
      $this->last = $result; 
     } 
    } 

    /** 
    * Get the rows from the most recently executed query, excluding 
    * cached queries 
    * @return array 
    * 
    */ 
    public function getRows() { 
     return $this->last->fetch_array(MYSQLI_ASSOC); 
    } 

    /** 
    * Delete records from the database 
    * @param String the table to remove rows from 
    * @param String the condition for which rows are to be removed 
    * @param int the number of rows to be removed 
    * @return void 
    * 
    */ 
    public function deleteRecords($table, $condition, $limit) { 
     $limit = ($limit == '') ? '' : ' LIMIT ' . $limit; 
     $delete = "DELETE FROM {$table} WHERE {$condition} {$limit}"; 
     $this->executeQuery($delete); 
    } 

    /** 
    * Update records in the database 
    * @param String the table 
    * @param array of changes field => value 
    * @param String the condition 
    * @return bool 
    * 
    */ 
    public function updateRecords($table, $changes, $condition) { 
     $update = "UPDATE " . $table . " SET "; 
     foreach($changes as $field => $value) { 
      $update .= "`" . $field . "`='{$value}',"; 
     } 
     // remove our trailing , 
     $update = substr($update, 0, - 1); 
     if ($condition != '') { 
      $update .= "WHERE " . $condition; 
     } 
     $this->executeQuery($update); 
     return true; 
    } 

    /** 
    * Insert records into the database 
    * @param String the database table 
    * @param array data to insert field => value 
    * @return bool 
    * 
    */ 
    public function insertRecords($table, $data) { 
     // setup some variables for fields and values 
     $fields = ""; 
     $values = ""; 
     // populate them 
     foreach($data as $f => $v) { 
      $fields .= "`$f`,"; 
      $values .= (is_numeric($v) && (intval($v) == $v)) ? $v . "," : "'$v',"; 
     } 
     // remove our trailing , 
     $fields = substr($fields, 0, - 1); 
     // remove our trailing , 
     $values = substr($values, 0, - 1); 
     $insert = "INSERT INTO $table ({$fields}) VALUES({$values})"; 
     // echo $insert; 
     $this->executeQuery($insert); 
     return true; 
    } 

    /** 
    * Sanitize data 
    * @param String the data to be sanitized 
    * @return String the sanitized data 
    *   
    */ 
    public function sanitizeData($value) { 
     // Stripslashes 
     if (get_magic_quotes_gpc()) { 
      $value = stripslashes($value); 
     } 
     // Quote value 
     if (version_compare(phpversion(), "4.3.0") == "-1") { 
      $value = $this->connections[$this->activeConnection]->escape_string($value); 
     } else { 
      $value = $this->connections[$this->activeConnection]->real_escape_string($value); 
     } 
     return $value; 
    } 

    /** 
    * Get the rows from the most recently executed query, excluding 
    * cached queries 
    * @return array 
    * 
    */ 
    public function getRows() { 
     return $this->last->fetch_array(MYSQLI_ASSOC); 
    } 

    public function numRows() { 
     return $this->last->num_rows; 
    } 

    /** 
    * Gets the number of affected rows from the previous query 
    * @return int the number of affected rows 
    *   
    */ 
    public function affectedRows() { 
     return $this->last->affected_rows; 
    } 

    /** 
    * Deconstruct the object 
    * close all of the database connections 
    */ 
    public function __deconstruct() { 
     foreach($this->connections as $connection) { 
      $connection->close(); 
     } 
    } 
} 

使用糟糕据我所知的主要目的是允许应用程序很容易进化 到不断变化的要求(来源:Design Patterns explained a new persective)和它们在本书中给出了一些很好的例子 来解释这一点。

所以我的问题是,在这种情况下,应用程序需求中可能发生什么变化需要使用数据库管理的类 ,而不是让我们说使用一堆函数来完成工作?

在数据库类驻留在DB_driver.phpDB_active_rec.php. 有什么区别将使其如果他们把这些数据库的功能在帮助文件,而不是我已经看到了同样的事情在codeigniter一个将它放在一个库?

我已经通过在这个论坛上给出的答案走了,但他们没有回答 从一个要求点这个问题

谢谢..

+0

只是尝试使用函数来实现它并进行比较。 – zerkms

回答

2

把里面的逻辑是班主要是为便于组织但是在共享上下文中如何共享数据时有一些优点。您可以在将代码封装到不同的类(如dbtemplate)中时创建代码的物理分隔,而不像它们在仅包含函数的多个文件中。类允许将功能组合在一起并设置数据共享的范围。

如果你需要任何例子让我知道。但你似乎知道你在说什么。

根据您的评论以下我想到一些更多的事情,我可以添加到这篇文章。

当你开始思考OO时,你的思维会发生范式转变。在它开始有意义之前,它必须发生在你的大脑中。如果你继续认为程序化就像我需要这样做,所以我需要一个功能。认为我需要这样做,为什么?它是什么?然后用它构建一个对象,并想我还能用它做什么?我怎样才能组织好,所以我也可以与别人分享。 If when i do other things with it if there is a common resource i need to access how do i keep reference to it.

+0

就组织易用性而言,codeigniter似乎有很多帮助文件,其中只包含函数,整个体系结构仍然看起来很干净......至于“设置数据共享范围”,我假设你正在谈论全球数据的缺点,但这足以创造一个单独的班级的原因..你不认为这是一个矫枉过正的? – depz123

+0

设置datascope是一个很大的要求。我个人并不认为它的过度杀伤力。取决于你来自哪里。 – DevZer0

+0

我为你添加了一些评论 – DevZer0