为什么在创建登录模型时Yii调用login()方法 [英] Why Yii calls login() method, when creating Login model

查看:136
本文介绍了为什么在创建登录模型时Yii调用login()方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我之前的问题之后,我发现,在我的每个Yii项目中(自动生成了登录代码)由Yii在最初的应用程序生成时执行,并且没有被我更改),Yii在创建此模型时从Login表单模型执行login()方法.为什么?目的是什么?

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?

我已经分析了应用程序流程的逐步执行(使用Netbeans + XDebug + NetBeans Connector),似乎在创建Login模型/类($model = new Login;)时,Yii会自动加载两个类(CFormModel),然后在加载第二个后立即跳转到login()方法执行.两者之间没有任何步骤.

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.

这是我观察到的应用程序流程(我在Netbeans中始终使用Step Into (F7)):

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

    中的
  1. $model = new Login;(因为Yii::app()->user->isGuesttrue).

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

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

if(strpos($className,'\\')===false)用相同的方法(第403行).

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

if(self::$enableIncludePath===false)如上(第405行).

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

{.

与上面的第2点相同.

{.

return true;YiiBase.php中的autoload($className)(第435行).

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

return class_exists($className,false) || interface_exists($className,false);用相同的方法(第433行).

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

if($this->_identity === NULL).

(所有行均引用YiiBase.php)

有人可以解释一下,为什么Yii在创建Login表单模型时执行login()方法吗?

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

更多细节-尝试登录用户的目的是什么,如果没有可在登录过程中使用的数据-$this->login$this->password为空,则不会生成任何形式,因此用户没有机会输入任何内容,是否从未提交过POST数据($_POST为空,$_POST['Login']未定义)?

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)?

最好的部分是在那之后.如果您运行应用程序(此代码或任何由Yii自动生成且未更改登录代码的应用程序),则不会发生任何事情-Yii会正确显示登录表单(尽管尝试登录),但此过程肯定会失败.但是,如果逐步调试应用程序,则不会呈现任何形式,应用程序流将终止,并且脚本执行将死于Property "CWebApplication._identity" is not definedProperty "CHttpSession._identity" is not defined异常.

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?

推荐答案

由于我模型的NameLogin,因此其中定义的login()方法将PHP用作构造函数.

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

这与Yii无关,这是一种基本的PHP行为,它是从旧的,不良的PHP4继承而来的,仅在命名空间类的情况下才从PHP 5.3.3开始废弃.当类定义不使用名称空间时,此行为(将构造函数定义为与类具有相同名称的函数)仍然有效.

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.

有关更多详细信息,请参见此答案或参阅PHP手册.

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

这篇关于为什么在创建登录模型时Yii调用login()方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆