Smarty3 已经不能从 Smarty 类继承了。具体原因没有研究过,也没有必要 你可以 class Tpl { function __construct() { $this->Smarty = new Smarty; $this->Smarty->setTemplateDir ( __ROOT__."/View" ); $this->Smarty->setCompileDir (__ROOT__."./comp_c" ); $this->Smarty->left_delimiter = "<{"; $this->Smarty->right_delimiter = "}>"; } function __call($method, $param) { return call_user_func_array( array($this->Smarty, $method), $param); } }效果是一样的
感谢大大的解答,但是,新问题又来了。按照大大的写法 class Tpl { public $Smarty; function __construct() { $this->Smarty = new Smarty (); $this->Smarty->setTemplateDir ( "./View" ); $this->Smarty->setCompileDir ( "./comp_c" ); $this->Smarty->left_delimiter = "<{"; $this->Smarty->right_delimiter = "}>"; } function __call($method, $param) { return call_user_func_array ( array ($this->Smarty, $method ), $param ); } }前台调用class Index extends Tpl { public $Smarty = NULL; function index(){ $this->Smarty->assign('m','asdfasdfasdf');
$this->Smarty->display('index.html'); } } $tpl = new Index(); $tpl->index();不知道为什么Index类中的写在index()方法里smarty assign()不知为何调用不了… __call()魔术方法好像没起作用啊,大大求解答… Fatal error: Call to a member function assign() on a non-object in D:\AppServ\www\smarty\index.php on line 6
但是按大大这样写的话,又有另一个问题,所以我才 $this->Smarty->assign('m','asdfasdfasdf');这样写… 所以没有理解大大的话, 如果用$this->assign('m','asdfasdfasdf') 这样写的话,也会出现问题 报错是:Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'Array' was given in D:\AppServ\www\smarty\init.inc.php on line 14
你可以
class Tpl {
function __construct() {
$this->Smarty = new Smarty;
$this->Smarty->setTemplateDir ( __ROOT__."/View" );
$this->Smarty->setCompileDir (__ROOT__."./comp_c" );
$this->Smarty->left_delimiter = "<{";
$this->Smarty->right_delimiter = "}>";
}
function __call($method, $param) {
return call_user_func_array( array($this->Smarty, $method), $param);
}
}效果是一样的
class Tpl {
public $Smarty;
function __construct() {
$this->Smarty = new Smarty ();
$this->Smarty->setTemplateDir ( "./View" );
$this->Smarty->setCompileDir ( "./comp_c" );
$this->Smarty->left_delimiter = "<{";
$this->Smarty->right_delimiter = "}>";
}
function __call($method, $param) {
return call_user_func_array ( array ($this->Smarty, $method ), $param );
}
}前台调用class Index extends Tpl {
public $Smarty = NULL;
function index(){
$this->Smarty->assign('m','asdfasdfasdf');
$this->Smarty->display('index.html');
}
}
$tpl = new Index();
$tpl->index();不知道为什么Index类中的写在index()方法里smarty assign()不知为何调用不了… __call()魔术方法好像没起作用啊,大大求解答…
Fatal error: Call to a member function assign() on a non-object in D:\AppServ\www\smarty\index.php on line 6
你 $this->Smarty->assign('m','asdfasdfasdf'); 并没有用到魔术方法
我写 __call 原意是 $this->assign('m','asdfasdfasdf'); 这样调用的
但是按大大这样写的话,又有另一个问题,所以我才
$this->Smarty->assign('m','asdfasdfasdf');这样写… 所以没有理解大大的话,
如果用$this->assign('m','asdfasdfasdf') 这样写的话,也会出现问题 报错是:Warning: call_user_func_array() [function.call-user-func-array]: First argument is expected to be a valid callback, 'Array' was given in D:\AppServ\www\smarty\init.inc.php on line 14
应该是没有实例化成功,即 $this->Smarty 不是 Smarty 对象