2012-03-22 84 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);