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):
$model = new Login;
inactionLogin()
(sinceYii::app()->user->isGuest
istrue
).if(isset(self::$classMap[$className]))
inautoload()
inYiiBase.php
(line 396).if(strpos($className,'\\')===false)
in the same method (line 403).if(self::$enableIncludePath===false)
as above (line 405).{
below the begining ofclass Login extends CFormModel
definition.Same as point 2. above.
{
below the begining ofclass CFormModel extends CModel
definition.return true;
inautoload($className)
inYiiBase.php
(line 435).return class_exists($className,false) || interface_exists($className,false);
in the same method (line 433).if($this->_identity === NULL)
inlogin()
method inLogin
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?
Since my model's
Name
isLogin
, thenlogin()
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.