$this 在 zend 框架中的 .phtml 文件中如何工作?

编程入门 行业动态 更新时间:2024-10-25 06:30:14
本文介绍了$this 在 zend 框架中的 .phtml 文件中如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送ChatGPT账号..

在我所有的面向对象编程实践中,我从未在类定义之外使用过 $this.

For all my practice with OOP I have never used $this outside of the class definition.

虽然在 zendframework 中我们在视图模板文件中使用 $this,但显然它不是类定义的范围.我想知道它是如何实施的?我用谷歌搜索了很多,但没有运气.

While in zendframework we use $this in view template files, obviously it is not the scope of a class definition. I wonder how it has been implemented ? I have googled a lot but got no luck.

我想知道 zendframework 如何使用 $this 呈现其视图文件的机制.

I want to know the mechanism how zendframework renders it's view files with $this.

推荐答案

它实际上在类定义的范围内.简单的测试用例:

It actually is in the scope of a class definition. Simple test-case:

<?php
// let's call this view.php
class View {
   private $variable = 'value';
   public function render( ) {
       ob_start( );
       include 'my-view.php';
       $content = ob_get_clean( );
       return $content;
   }
}
$view = new View;
echo $view->render( );

现在,创建另一个文件:

Now, create another file:

<?php
// let's call this my-view.php.
<h1>Private variable: <?php echo $this->variable; ?></h1>

现在去访问view.php,你会看到my-view.php可以访问View类的私有变量.通过使用 include,您实际上将 PHP 文件加载到当前作用域中.

Now go and visit view.php, and you will see that my-view.php had access to the private variable of the View class. By using include, you actually load the PHP file into the current scope.

这篇关于$this 在 zend 框架中的 .phtml 文件中如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

更多推荐

[db:关键词]

本文发布于:2023-04-30 16:40:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1396282.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:框架   文件   工作   zend   phtml

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!