Why Yii calls login() method, when creating Login

2019-08-04 13:15发布

Following my previous question, I discovered, that in every of my Yii project (where login code was auto-generated by Yii upon initial application generation and not changed by me), Yii executes login() method from Login form model, when this model is created. Why? What is the purpose?

I've analysed step-by-step execution of application flow (with Netbeans + XDebug + NetBeans Connector) and it seems, that when creating Login model / class ($model = new Login;) Yii autoloads two classes (Login and CFormModel) and right after loading the second one, it jumps directly to login() method execution. No steps in between.

This is the application flow, that I observed (I'm always using Step Into (F7) in Netbeans):

  1. $model = new Login; in actionLogin() (since Yii::app()->user->isGuest is true).

  2. if(isset(self::$classMap[$className])) in autoload() in YiiBase.php (line 396).

  3. if(strpos($className,'\\')===false) in the same method (line 403).

  4. if(self::$enableIncludePath===false) as above (line 405).

  5. { below the begining of class Login extends CFormModel definition.

  6. Same as point 2. above.

  7. { below the begining of class CFormModel extends CModel definition.

  8. return true; in autoload($className) in YiiBase.php (line 435).

  9. return class_exists($className,false) || interface_exists($className,false); in the same method (line 433).

  10. if($this->_identity === NULL) in login() method in Login model (class).

    (all lines referes to YiiBase.php class)

Can someone explain me, why Yii is executing login() method upon creation of Login form model?

In more details -- what is the purpose of attempting to login user, if there no data, that can be used in login process -- $this->login and $this->password are null, there was no form ever generated, so user hasn't got chance to enter anything, no POST data was ever submitted ($_POST is empty, $_POST['Login'] is undefined)?

The best part goes after that. If you run application (this or any auto-generated by Yii with unchanged login code), nothing happens -- Yii correctly displays login form, though login attempt, that proceeded render for sure failed. But, if you debug application step-by-step, no form is ever rendered, application flow is terminated and script execution dies on: Property "CWebApplication._identity" is not defined and Property "CHttpSession._identity" is not defined exceptions. Something, that we could expect, since CWebApplication._identity is undefined in this context.

Can someone explain me, what is going on here?

标签: php login yii
1条回答
何必那么认真
2楼-- · 2019-08-04 13:39

Since my model's Name is Login, then login() method defined in it, acts for PHP as constructor.

This has nothing to do with Yii, this is a base PHP behavior, inherited from old, bad PHP4 and depracated as of PHP 5.3.3 only in case of namespaced classes. When class definition does not use namespace, this behavior (defining constructor as function named the same way as class) is still valid.

See this answer for more details or refer to PHP manual.

查看更多
登录 后发表回答