<?php
class Student
{
protected static $possibleFilters = ['name', 'id', 'age']; // ...
protected $filters = [];
public static function __callStatic($method, $value)
{
$self = new static;
if(self::checkMethod($method)) {
return $self->$method($value);
}
return false;
}
public function __call($method, $value)
{
if($filter = self::checkMethod($method)) {
$this->filters[$filter] = $value;
}
return $this;
}
public function get()
{
foreach($this->filters as $filter) {
// ...
}
// return $students....
return [];
}
protected static function checkMethod($method)
{
if(strpos($method, 'by') === 0) { $filter = lcfirst
(substr($method, 2));
if(in_array($filter, static
::$possibleFilters)) { return $filter;
}
}
return false;
}
}
$students = Student::byName('Joe')->byAge(12)->get();
PD9waHAKCmNsYXNzIFN0dWRlbnQKewoJcHJvdGVjdGVkIHN0YXRpYyAkcG9zc2libGVGaWx0ZXJzID0gWyduYW1lJywgJ2lkJywgJ2FnZSddOyAvLyAuLi4KCQoJcHJvdGVjdGVkICRmaWx0ZXJzID0gW107CgkKCXB1YmxpYyBzdGF0aWMgZnVuY3Rpb24gX19jYWxsU3RhdGljKCRtZXRob2QsICR2YWx1ZSkKCXsKCQkkc2VsZiA9IG5ldyBzdGF0aWM7CgkJCgkJaWYoc2VsZjo6Y2hlY2tNZXRob2QoJG1ldGhvZCkpIHsKCQkJcmV0dXJuICRzZWxmLT4kbWV0aG9kKCR2YWx1ZSk7CgkJfQoJCQoJCXJldHVybiBmYWxzZTsKCQoJfQoJCglwdWJsaWMgZnVuY3Rpb24gX19jYWxsKCRtZXRob2QsICR2YWx1ZSkKCXsKCQlpZigkZmlsdGVyID0gc2VsZjo6Y2hlY2tNZXRob2QoJG1ldGhvZCkpIHsKCQkJJHRoaXMtPmZpbHRlcnNbJGZpbHRlcl0gPSAkdmFsdWU7CgkJfQoJCQoJCXJldHVybiAkdGhpczsKCX0KCQoJcHVibGljIGZ1bmN0aW9uIGdldCgpCgl7CgkJZm9yZWFjaCgkdGhpcy0+ZmlsdGVycyBhcyAkZmlsdGVyKSB7CgkJCS8vIC4uLgoJCX0KCQkKCQkvLyByZXR1cm4gJH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