[英]How does Zend framework manage to use $this outside of a class?
I am fairly new to OOP in PHP, and I tried to create my own layout class. 我对使用PHP进行OOP相当陌生,并且尝试创建自己的布局类。
In my case, it works like this: 就我而言,它的工作方式如下:
$layout = new Layout('some name'); // title
$layout->setStyle('somestyle.css');
$layout->addDiv('id/class', $content );
Then, just in order to see how professionals create templating applications, I started reading Zend website, the manual part where creation of layouts is covered , but I am failing to understand how is it possible that they have a separate PHP file to initialize the layout, but the layout itself is located in a regular html file. 然后,为了了解专业人士如何创建模板应用程序,我开始阅读Zend网站,该手册涵盖了创建布局的手册部分,但是我无法理解他们如何有单独的PHP文件来初始化布局,但布局本身位于常规html文件中。
In that file, they are using $this
variable among HTML tags, but how can they do it without triggering 'use of $this variable outside of class context' error? 在该文件中,他们在HTML标记中使用
$this
变量,但是如何做到这一点而又不会触发“在类上下文之外使用$ this变量”错误? As far as I can see, the tags are not internally included into any class, nor is any PHP file included into the template file. 据我所知,标签没有内部包含在任何类中,也没有任何PHP文件包含在模板文件中。
Could you please provide a simple explanation / example of how this works? 您能提供一个简单的解释/示例如何工作吗?
template.php template.php
<p><?php echo $this->foo(); ?></p>
class.php class.php
class Bar {
public function render() {
include 'template.php';
}
public function foo() {
return 'foo';
}
}
$bar = new Bar;
$bar->render();
This is how. 就是这样 The template file is included in a class method.
模板文件包含在类方法中。
They're using include()
and/or require
. 他们正在使用
include()
和/或require
。 Anything that is loaded up via those functions acts as if it was literally cut&pasted into calling code. 通过这些函数加载的所有内容的行为就好像是被剪切并粘贴到调用代码中一样。 eg
例如
includeme.php: includeme.php:
<?php
$var = 'foo';
Some other file: 其他一些文件:
<?php
function bar() {
include('includeme.php');
echo $var; // ouputs "foo"
}
echo $var; // outputs "PHP Notice: Undefined variable: var
In object code: 在目标代码中:
includemephp: includemephp:
<?php
$this->var = 'foo';
echo $this->whatever;
And some other file: 和其他一些文件:
class foo {
public $var;
public $whatever = 'yoohoo!';
function bar() {
include('includeme.php');
echo $this->var;
}
}
$x = new foo();
$x->bar(); // outputs `yoohoo!foo`
Since the included text is DIRECTLY inserted into the code at the point include() is called, as if the contents of the included file were literally cut/pasted at that spot, there is no difference for $this
inside the included file - it's operating inside that method call, and $this will work as expected. 由于在调用include()时将包含的文本直接插入到代码中,就好像在该位置实际剪切/粘贴了包含文件的内容一样,包含文件中的
$this
没有区别-它正在运行在该方法调用中,$ this将按预期工作。
But if you had a simple file like this: 但是,如果您有一个像这样的简单文件:
<?php
include('includeme.php');
You will get the Using $this when not in object context
error, because there is no object/method surrounding the included code. 您将得到
Using $this when not in object context
错误,因为所包含的代码周围没有对象/方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.