在php的文件包含,一般都是引入类文件,有点像加入类库的方式,现在探讨的是一种在php对象方法中require文件的特色。
在对象A中的a()方法中,require文件B,那么,在B文件中是可以使用$this->xxx(),
验证:我们在test/文件夹下面新建了两个文件 index.php 和 test.php
index.php文件内容如下:
<?php
class test{
public function index(){
echo 111;
$this->test1();
}
public function test1(){
require("./test.php");
}
public function xx(){
echo 3333;
}
}
$test = new test();
$test->index();
test.php:
fdssafsdafads <?php echo $this->xx(); ?>
访问 http://xx.com/test/index.php
可以看到输出结果:
111fdssafsdafads 3333
也就是说,$this 就是$test对象,
参看文件:yii2 页面功能块配置实现原理(前端+后端提供数据类),以及Views深度嵌套
结合yii2 view的利用ob函数生成view的原理,就会更加透彻。