2012-03-22 87 views
2

的index.php

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

function do_something() { 
    global $Smarty; 
     echo "where is smarty?" 

    var_dump($Smarty); 
    $ObjSmarty->assign("teams_list", $teams_list); 
} 
get_active_teams(); 

没有转储和错误分配...

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

function do_something() { 
    global $Smarty; 
     echo "where is smarty?" 

    var_dump($GLOBALS); 
    var_dump($GLOBALS["Smarty"]); 
} 
get_active_teams(); 

转储全局的显示Smarty的,当我倾倒$全局[ “智者”]没有。这是怎么回事。

我没有课是这个问题吗?

如何在不使用声明类的情况下分配给加载的php内部smarty对象?

+0

您在第一个例子中将其称为$ Smarty和$ ObjSmarty - 是错误还是实际存在于代码中? – 2012-03-22 09:07:44

回答

3

您是否在某处调用了do_something函数? 也许你可以这样做:

function do_something($Smarty) { 
    // ... Do something with smarty here... 
} 
do_something($Smarty); 
2

使用全局变量是不是一个很好的主意,为什么不通过$ Smarty的作为函数参数?

function foo($smarty) { 
    var_dump($smarty); 
} 

foo($smarty);