问题描述
限时送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:关键词]
发布评论