Re: new keyword combined with other things...

From: Date: Wed, 08 Dec 2010 15:14:39 +0000
Subject: Re: new keyword combined with other things...
References: 1 2 3 4  Groups: php.general 
Request: Send a blank email to php-general+get-309907@lists.php.net to get a copy of this message
On Tue, Dec 7, 2010 at 1:55 PM, Paul M Foster <paulf@quillandmouse.com> wrote: > > > $this->db->select('title')->from('mytable')->where('id', > $id)->limit(10, > 20); > > What kind of internal magic they use to make this work, I don't know. I > haven't examined their internals. > > Paul > > -- > Paul M. Foster > I've never seen the code nor used CodeIgniter but it might be something like this: class DB extends Abstraction { private $sqlString; private $hasWhere; public function select($fields) { if (is_array($fields)) $this->sqlString = 'select '.implode(',' $fields); else $this->sqlString = 'select '.$fields; return $this; } public function from($from) { $this->sqlString .= ' from '.$from; return $this; } public function where($field, $value, $isAnd = false) { if ($this->hasWhere) $this->sqlString .= $isAnd ? ' and ' : ' or '; else { $this->hasWhere = true; $this->sqlString .= ' where '; } $this->sqlString .= $field.' = '.$this->escape($value); return $this; } public function limit($start, $rows) { $this->sqlString .= " limit {$start}, {$rows}"; return $this; } // other methods } The only need I see for this is to build the query dynamically... which should be very rare since all those function calls are expensive, IMO. Regards, Tommy

« previous php.general (#309907) next »