I am trying to build a database-class that allows method chaining to query it. I want to instantiate it like Laravel's Eloquent by making the first call static.
See this example:
class Database
{
public function construct($data) {
$this->data = $data;
}
public function filter($criteria) {
// ...
return $this;
}
public static function filter($data, $criteria) {
$obj = new Database($data);
$obj->filter($criteria);
return $obj;
}
public function add($value) {
// ...
return $this;
}
public static function add($data, $value,) {
$obj = new Database($data);
$obj->add($value);
return $obj;
}
}
This would allow me for example:
Database::add($myData, $newValue)->add($anotherValue)->filter('isString');
Database::filter($myData, 'isNumber')->add($thirdValue);
This is not supposed to be the real thing, but I am curious if there was a way to reduce the duplicated code throughout my static methods or to remove them entirely.
I thought of the magic method __callStatic() but I am not sure if it is the best way to achieve it.
I would appreciate it if someone who knows could explain me, how big frameworks deal with this kind of task.