简体   繁体   English

基本的Yii概念:$这个定义在哪里?

[英]Basic Yii Concept: Where is $this defined?

I'm taking a look at Yii Framework's tutorial about a blog application. 我正在看一下Yii Framework关于博客应用程序的教程。 I'm in the process of understanding, but I don't understand one major component: this is found in the beginning of a particular .php view file. 我正在理解,但我不理解一个主要组件:这是在特定.php 视图文件的开头找到的。

<?php
$this->breadcrumbs=array(
    'Manage Posts',
);
?>

I'd just like to know where does $this come from. 我只想知道$this来自哪里。 As far as I understand, $this can only be used if it is within the scope of a class. 据我所知, $this只能在类的范围内使用。 However, I see no class implemented here, so can anyone enlighten me on how Yii does this for me? 但是,我看到这里没有实现任何课程,所以有人可以告诉我Yii如何为我做这个吗?

$this here refers to the current controller class. $this这里指的是当前的控制器类。 If you see the controller in components/Controller.php , you'll also see a $breadCrumbs=array(); 如果你在components / Controller.php中看到控制器 ,你还会看到$breadCrumbs=array(); . See definitive guide to views : 请参阅权威指南

Inside the view script, we can access the controller instance using $this . 在视图脚本中,我们可以使用$ this访问控制器实例。 We can thus pull in any property of the controller by evaluating $this->propertyName in the view. 因此,我们可以通过在视图中评估$ this-> propertyName来获取控制器的任何属性。

The controller renders a view ultimately using, renderInternal . 控制器最终使用renderInternal呈现视图。 And if you see the source of that function, you'll see php's require() : 如果你看到该函数的来源,你会看到php的require()

public function renderInternal($_viewFile_,$_data_=null,$_return_=false)
{
    // we use special variable names here to avoid conflict when extracting data
    if(is_array($_data_))
        extract($_data_,EXTR_PREFIX_SAME,'data');
    else
        $data=$_data_;
    if($_return_)
    {
        ob_start();
        ob_implicit_flush(false);
        require($_viewFile_);
        return ob_get_clean();
    }
    else
        require($_viewFile_);
}

And because require is used, $this is available at that point to the code being included: 并且因为使用了require,所以此时可以使用$ this包含的代码:

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. 包含文件时,它包含的代码将继承发生包含的行的变量范围。 Any variables available at that line in the calling file will be available within the called file, from that point forward. 从那时起,调用文件中该行可用的任何变量都将在被调用文件中可用。 However, all functions and classes defined in the included file have the global scope. 但是,包含文件中定义的所有函数和类都具有全局范围。

Hence when we do $this->render('view'); 因此当我们做$this->render('view'); the view will have access to $ this . view可以访问$ this

I'm pretty sure Yii compiles templates into classes, so at runtime you actually do have a class. 我很确定Yii会将模板编译成类,所以在运行时你确实有一个类。 Check __FILE__ and get_class($this) for details. 检查__FILE__get_class($this)以获取详细信息。

If this template is just require d inside a method, it actually goes into object scope. 如果这个模板只是require在方法内部,它实际上进入对象范围。 So this stands for some View object that renderes the temlate. 所以this代表一些渲染temlate的View对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM