Jump to content

Recommended Posts

Well I am trying to design a class called database, which will significantly reduces the amount of codes/lines running mysql query in a script file. The object methods are basically mysql operations such as create(), select(), update(), insert(), delete() and so on. I have two possible approaches to execute mysql queries, and lemme illustrate point with the 'select from table' method:

 

Approach 1: Write everything inside a gigantic method, such as select()

 

class Database{
   // codes
   public function select($table, $columns, $index, $where, $comparison, $logic, $returntype, $extra){
  // codes to return a highly customized query string, then execute this query, and finally returns any type as specified in $returntype(field, string, row, assoc, array or object)
  // the codes are extremely long here...
  } 
  // more codes
}

// to return an array, for instance, the following line may be written to retrieve database info:
$db = new Database($databasehost, $userName, $userPassword, $databaseName);
$array = $db->select("users", array("password", "email"), true, array("id" => 1, "username" => "admin"), "=", "AND","ARRAY", "LIMIT = 1"); 

 

Approach 2: Use Object Method Chaining

class Database{
  private $method, $table, $column, $where, $comparison, $logic, $limit, $returntype;
  public function select($column == "*"){
  // simple codes here
  }
  public function table($table){
  // simple codes here
  }
  public function where($where){
  // simple codes here
  }
  public function comparison($comparison){
  // simple codes here
  }
  public function logic($logic){
  // simple codes here
  }
  public function limit($limit){
  // simple codes here
  }
  public function result($method){
  // execute the query and is ready to fetch
  }
  public function fetch($returntype){
  // fetch rows, assoc, array, objects or else based on return type
  }
}
  
// to return an array, for instance, we execute multiple methods altogether:
$db = new Database($databasehost, $userName, $userPassword, $databaseName);
$array = $db->select(array("password", "email"))->table("user")->where(array("id" => 1, "username" => "admin" )) -> comparison("=") -> logic("AND") => limit(1) -> result() -> fetch("ARRAY");

 

Both approaches are likely to work on my site, but I do not know which one is considered more professional and which one runs better performance-wise. The first approach is quite complicated to use for another programmer, but it may be more efficient. The second approach is more readable and flexible, while I wonder if running a chain of object methods will consume a huge amount of CPU and bandwidth. Can anyone of you please gimme your points of view on this? Thank you so much.

Link to comment
https://forums.phpfreaks.com/topic/247250-regarding-php-objects-method-chaining/
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.