2011-06-21 36 views
0

我有一個調用模板文件,並需要檢查文件中指定的參數的函數調用的文件裏面:在PHP中得到的參數從外部

注:只有縮短例子在這裏

裏面config.php(在一個類中) - 編輯:這應該檢查是否$echotrue並且它是否在模板中設置。

// Edit: 
function check_cb() 
{ 
    // The {$echo} is meant to be from inside the template 
    if ($echo === TRUE AND $inside_template === TRUE) 
     return $whatever = 'Using {$echo} in a callback & from inside template is not allowed.' 

    return $whatever = 'Check: ok'; 
} 

內的template.php(check_cb被調用前)

$echo = FALSE; 
$args = array(
    'type'   => 'input' 
    ,'id'   => 'input_template_UID' 
    ,'label'  => 'Input Template Label' 
    ,'label_sep' => false 
    ,'opt_name'  => 'abc_xyz' 
    ,'value'  => 'test value' 
); 
example_function($args, $echo); 

內的另一個類

問題的一部分:我怎樣才能得到的參數從一個文件裏面?它甚至有可能嗎? 增加:我也可以修改example_function(),但目前我還沒有任何好的想法。

回答

2

只需在config.php文件的開頭添加此include_once("template.php");即可。 另請在您的check_cb()功能中加入global $echo;

事情是這樣的:

include_once("template.php"); 
function check_cb() 
{ 
    global $echo; 
    // The {$echo} is meant to be from inside the template 
    if ($echo === false) 
     return $whatever = 'no' 

    return $whatever = 'yes'; 
} 

include_once("template.php")基本上都會使可見的變量從template.phpconfig.php您的所有功能。但要使用變量定義在你的函數之外,你必須使用global keywor。

有關global的更多信息,請使用herehere作爲include_once聲明。

+0

你是說我應該在每個模板中設置回顯全局?順便說一句:我更新了Q更清晰(第一個功能+解釋)。 – kaiser

+0

噢,對不起:我以一種容易被誤解的方式編寫Q,並且信息太少 - 通過編輯來修復。 – kaiser