【问题标题】:How to assign $smarty object to to php function?如何将 $smarty 对象分配给 php 函数?
【发布时间】:2012-03-22 06:57:19
【问题描述】:

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,当我转储 $globals["smarty"] 时什么也没有。怎么回事。

我没有课是这个问题吗?

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

【问题讨论】:

  • 您在第一个示例中将其称为 $Smarty 和 $ObjSmarty - 这是您的代码中的拼写错误还是实际存在?

标签: php function smarty


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    使用全局变量不是一个好主意,为什么不将 $Smarty 作为函数参数传递呢?

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

    【讨论】:

      猜你喜欢
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多