Zend框架中的"$this"是什么意思?

10

我刚接触Zend框架,有一个基础问题。

假设我在使用layout.phtml或任何脚本的index.phtml。

当我使用"$this->"时,我指的是哪个实例?

我在一本书中读到以下内容: "$this在模板文件中可用,它是访问Zend_View功能的入口"。

这是否意味着我可以访问library/Zend/View/Helpers目录中任何文件内的任何类的方法?

如果这个问题太愚蠢或者太简单,请原谅。

谢谢

5个回答

15

$this-> 在视图模板中是对您在控制器中创建的 Zend_view 对象的引用。

尝试在模板中使用 var_dump($this)print_r($this) (在 print_r 前加上 <pre> 以获得更好的格式化)。变量转储可能会帮助您更好地理解发生了什么。


4
当您在.phtml文件中使用$this时,您正在引用Zend_View的实例。这个对象是由控制器对象为您设置的,它是Zend_Controller_Action的一个实例。 Zend_Controller_Action确保您的视图对象可以访问它所需的任何视图助手。因此,通过$ this变量,您确实可以访问库/Zend/View/Helpers目录中的任何助手。
您还可以通过$ this访问自己编写并放置在应用程序/views/helpers目录中的任何助手。请参阅有关编写自己的视图助手的手册。一旦开始,您将始终使用它们,因为这是一种非常简单而强大的方法来保持DRY代码

顺便提一下,你同样可以直接访问应用程序/视图/过滤器目录中放置的任何 过滤器,正如你可以从Zend/Controller/Action.php中的initView()的docblock看到的那样。

/**
 * Initialize View object
 *
 * Initializes $view if not otherwise a Zend_View_Interface.
 *
 * If $view is not otherwise set, instantiates a new Zend_View
 * object, using the 'views' subdirectory at the same level as the
 * controller directory for the current module as the base directory.
 * It uses this to set the following:
 * - script path = views/scripts/
 * - helper path = views/helpers/
 * - filter path = views/filters/
 *
 * @return Zend_View_Interface
 * @throws Zend_Controller_Exception if base view directory does not exist
 */
public function initView()

Zend Framework中从请求到响应的整个过程相当复杂。如果您感兴趣,可以在这里这里找到一些图表。
一旦您克服了陡峭的学习曲线,Zend Framework非常强大且易于使用。坚持下去是值得的,因为最终您会理解它,并因此更快地生成更好的代码。我曾经在文档API上苦苦挣扎,但发现最好的文档就是代码。现在,我将正在使用的任何ZF组件的代码打开在一个独立的netbeans窗口中,以便参考。
祝你在ZF中好运。

2

通常情况下,您会在控制器操作中使用类似以下方式将某些数据分配给视图对象:

$form = My_Form;
//assign My_Form to the view object
$this->view->form = $form;

在您的视图脚本中,通常会使用类似以下代码来访问该数据:
//this bit of code would display your whole form in the view script
//along with any layout information contained in your layout file
<?php echo $this->form ?>

同时,可以从Bootstrap中将项目分配给视图对象,并且这些项目将对布局或视图脚本可用。以下是一个例子:

 protected function _initView() {
        //Initialize view
        $view = new Zend_View();
        //get doctype from application.ini
        $view->doctype(Zend_Registry::get('config')->resources->view->doctype);
        $view->headTitle('Our Home');
        //get content-type from application.ini
        $view->headMeta()->appendHttpEquiv('Content-Type',
                Zend_Registry::get('config')->resources->view->contentType);
        //add css files
        $view->headLink()->setStylesheet('/css/blueprint/screen.css');
        $view->headLink()->appendStylesheet('/css/blueprint/print.css', 'print');
        $view->headLink()->appendStylesheet('/css/master.css');
        $view->headLink()->appendStylesheet('/css/main.css');
        $view->headLink()->appendStylesheet('/css/nav.css');

        //add it to the view renderer
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper(
                        'ViewRenderer');
        $viewRenderer->setView($view);
        //Return it, so that it can be stored by the bootstrap
        return $view;

现在,可以通过以下方式在layout.phtml中访问此数据:

<?php echo $this->doctype() . "\n"; ?>
<html>
    <head>
        <?php echo $this->headMeta() . "\n" ?>
        <?php echo $this->headLink() . "\n" ?>
        <!--[if lt IE 8]>
          <link rel="stylesheet" href="/css/blueprint/ie.css" type="text/css" media="screen, projection" />
        <![endif] -->
    </head>

为了完整起见,这里是 $this 的 PHP 手册版本:

在类方法中,可以使用 $this->property(其中 property 是属性的名称)的形式访问属性、常量和方法,除非在静态类方法的上下文中访问静态属性,在这种情况下,使用 self::$property 的形式访问。有关更多信息,请参见静态关键字。

伪变量 $this 在任何类方法内部可用,当该方法从对象上下文中调用时。$this 是对调用对象的引用(通常是方法所属的对象,但如果从辅助对象的上下文中静态调用该方法,则可能是另一个对象)。

这并不是一个完整的解释,但我希望它能让您有所启发。


非常好的回答,但实际上并没有涉及到提问者的问题。您解释了如何使用 $this 伪变量,而不是它所指代的对象。 - vascowhite
@vascowhite,你说得对。我虽然多次提到了"view"对象,但从未解释过它是什么。但是我认为,在我们两个人的帮助下,他已经接受了一个关于该对象是什么以及如何使用它的速成课程。:) 我敢打赌,我的回答就是他想要问的问题。 ;) - RockyFord

0

如所述,视图中的$this是Zend_View的实例。

请参见Zend_View类中的render方法。

   public function render($name)
    {
        // find the script file name using the parent private method
        $this->_file = $this->_script($name);
        unset($name); // remove $name from local scope

        ob_start();
        $this->_run($this->_file);

        return $this->_filter(ob_get_clean()); // filter output
    }

基本上,ZF操作辅助程序(ViewRnderer)创建一个Zend_View实例,并通过传递视图文件的名称(index.phtml)调用渲染方法。

$view = new Zend_View(); $view->render('index.phtml');

正如您所看到的,输出缓冲区在渲染方法中使用ob_start。 它在Zend_View类的上下文中加载index.phtml文件,因此可以在其中使用$this作为其代码是该类的一部分。


0
除了其他答案之外,$this 将帮助您使用在项目的 application/view/helper 目录中定义的辅助函数。您可以通过在初始化器中将它们与 zend_helper 绑定来在项目的 .phtml 或视图文件中的任何位置使用所有这些辅助函数。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接