Answer the question
In order to leave comments, you need to log in
What does the construct in the class call after Users::model() mean?
Greetings. I'm learning php, yii has this code out of the box:
$model=Users::model()->findByPk($id);
In this code, we are accessing the static model method of the Users class, but what comes next?
I even saw constructions like Users::model()->findByPk($id)-> something else;
I looked at good lessons on OOP PHP, nowhere it is told what it is....
Answer the question
In order to leave comments, you need to log in
judging by the Users::model() code, it returns some object on which the findByPk method is called with the $id parameter
User::model - read about static methods. Roughly speaking, this method, through late static binding, takes the name of the class through which we accessed this method (in the example it is User) and requests ... something like a repository (something like, because in yii everything is not like people , and this repository is also a metadata manager and a query builder ... to simplify). Further, the method simply returns the repository for the model with this class.
Next is method chaining. Each call to an object's method returns that same object. This is how the configuration is done.
Users::model() returns an object that has a findByPk($id) method, which is called immediately. Open the Users class and see what and how it works inside.
This is such a singleton
public static function model($className=__CLASS__)
{
if(isset(self::$_models[$className]))
return self::$_models[$className];
else
{
$model=self::$_models[$className]=new $className(null);
$model->attachBehaviors($model->behaviors());
return $model;
}
}
public function findByPk($pk,$condition='',$params=array())
{
Yii::trace(get_class($this).'.findByPk()','system.db.ar.CActiveRecord');
$prefix=$this->getTableAlias(true).'.';
$criteria=$this->getCommandBuilder()->createPkCriteria($this->getTableSchema(),$pk,$condition,$params,$prefix);
return $this->query($criteria);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question