从smarty文件结构部署看面向对象中路径的作用域

public function __construct()
{
// selfpointer needed by some other class methods
$this->smarty = $this;
if (is_callable('mb_internal_encoding')) {//多字节编码
mb_internal_encoding(SMARTY_RESOURCE_CHAR_SET);
}
$this->start_time = microtime(true);
// set default dirs DS在windows下是字符‘\’linux下是‘/’
$this->template_dir = array('.' . DS . 'templates' . DS);//即‘.\templates\’当前文件夹下的templates文件夹内
$this->compile_dir = '.' . DS . 'templates_c' . DS;//即‘.\templates_c\’
        $this->plugins_dir = array(SMARTY_PLUGINS_DIR);
$this->cache_dir = '.' . DS . 'cache' . DS;
$this->config_dir = array('.' . DS . 'configs' . DS);
$this->debug_tpl = 'file:' . dirname(__FILE__) . '/debug.tpl';
if (isset($_SERVER['SCRIPT_NAME'])) {
$this->assignGlobal('SCRIPT_NAME', $_SERVER['SCRIPT_NAME']);//当前脚本的名字
}
}

①上边的代码是Smarty类的构造函数源代码,所在文件是:E:\APMServ\www\htdocs\zf\two\smarty\Smarty.class.php

②我在另一个文件中(ZF的入口文件中)将该文件包含进来:E:\APMServ\www\htdocs\zf\two\index.php

<?php
·········
$registry = new Zend_Registry();

require_once('Smarty.class.php');
$views = new Smarty();

$registry->set('view', $views);
··········

③我在第三个文件中使用这个new出来的对象:E:\APMServ\www\htdocs\zf\two\application\controllers\HelloController.php

<?php
require_once('Zend/Controller/Action.php');
class HelloController extends Zend_Controller_Action
{
public function helloAction()
{
$smarty = Zend_Registry::get('view');

$smarty->assign('name','zhangsan');
$smarty->display('hello.phtml');
}

}

问题是:①中smarty的构造函数默认的templates和templates_c等等文件应该和smarty.class.php在同一文件夹下,那么我在②中new这个类生成对象,③文件中使用这个对象,那我到底应该把templates和templates_c等等文件夹建在那里呢

结果:应该与new这个类的语句所在的文件建在同级目录下,否则会出现:

SmartyException: Unable to load template file 'hello.phtml' in E:\APMServ\www\htdocs\zf\two\smarty\sysplugins\smarty_internal_templatebase.php

当然你没有用smarty默认的路径,而是另外指定的路径,那就看你指定到哪里了

原文地址:https://www.cnblogs.com/iLoveMyD/p/2387452.html