I want to be able to do something like this:
$table_object->getRows()->where($wer)->or($or)->orderBy('field', 'DESC');
If i were sure that all the methods will be called each time and in that order, then it would be simple and i can return an instance of the object itself on each method call so that the query gets build and finally executed at orderBy method. However I want the class to be able to also execute queries like so:
$table_object->getRows()->where($wer);
The following code would work for the first code example (ie when all methods are called) but not with the second one where only method where is called after getRows. It only returns an instance of itself.
class DatabaseTable extends Database
{
protected $table_name;
protected $query;
public function getRows()
{
return ($this instanceof self)? $this : false;
}
public function where(array $where)
{
foreach ($where as $field => $value){
$w[] = $field . ' = "' . $this->escapeString($value) . '"';
}
$this->query = "SELECT * FROM {$this->table_name} WHERE " . join($w, ' AND '));
return $this;
}
public function or(array $Clause)
{
foreach ($clause as $field => $value){
$o[] = $field . ' = "' . $this->escapeString($value) . '"';
}
$this->query .= join($w, ' AND ');
return $this;
}
public function orderBy($field, $type)
{
$this->query .= " ORDER BY $field $type ";
$this->executeQuery($this->query);
}
}
Ignore all minor errors - (i didnt check if it worked for sure, but it should.) how can I achieve this?