Answer the question
In order to leave comments, you need to log in
How to call and return the result of a chain in a constructor?
Hello.
I'm parsing ActiveRecord step by step and I can't understand how this principle works. If I return a chain in the constructor, then there is no data. If I pull from the outside, then everything works.
As I understand it, the problem is that the constructor cannot return anything and you need to call this chain outside the constructor. I can’t understand how ActiveRecord does it, I already got lost in their code.
Please point me in the right direction.
class Member{
static function find($params=null){
return new Query(get_called_class(),$params);
}
}
class Query{
public function __construct($className, $params = []){
if (is_numeric($params)) {
$this->where("id = ?", $params)->one();
}
}
/* where select etc... */
}
$member = Member::find(1); // Нет списка из базы
$member = Member::find()->where("id = ?", 1)->one(); // Есть список из базы
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question