0

提前感谢您的任何建议。

我有一个文件(内容文件),其中创建了一个数组,如下所示:

$config['plugins']['BannerRotate'] = array(
    'container'    =>    'abroadView',
    'arrows'    =>    true,
    'auto'        =>    true,
    'speed'        =>    '15000',
    'width'        =>    '300px',
    'height'    =>    '220px',
    'tags'      =>    'slider'
);

这反过来又被所谓的“模板系统”拾取,并以页面(和该数组)作为参数(非常标准)呈现布局。

然后模板系统使用该数组生成一个新对象,如下所示:

if(isset($GLOBALS['config']['plugins'])){
    foreach($GLOBALS['config']['plugins'] as $plugin => $ary){
        $$plugin = new Ispweb_Plugindaemon(CURRENTSRV,getcwd().'/',
        $GLOBALS['config']['plugins'][$plugin],$plugin);
        // this statement is simply the result of the eval statement below
    }
}

那么,由于本例中插件的名称是 BannerRotate,所以对象是 $BannerRotate(变量变量)。我这样做是为了每页可以有多个插件对象。然后使用该对象使用成员函数 $BannerRotate->getJS() 调用 jQuery 插件。这些成员函数调用位于模板系统内(重要)。

如果我在与初始数组 [OUTSIDE THE TEMPLATING SYSTEM] (我为首先创建对象而缓冲的文件)相同的文件中调用成员函数,那么一切都会消失。这对我来说没有意义,因为如果我使用 var_dump($BannerRotate),我会得到一个完整的对象。但是,假设在该内容文件中我执行 $BannerRotate->printNoscript(),一切都消失了,并且永远不会创建对象。然后我得到一个致命错误,我正在调用非对象的成员函数。那就是问题所在。

这是我在模板系统中为缓冲内容文件(并创建对象)所做的工作:

ob_start();
include $core_page_content; // the content file (where initial array is)
if(isset($GLOBALS['config']['plugins'])){
    foreach($GLOBALS['config']['plugins'] as $plugin => $ary){
        $ins[] = $plugin;
    }
}
$t = ob_get_contents();
ob_end_clean();
foreach($ins as $in){
    $a = CURRENTSRV; // a,b,c,d are just making the eval statement more clean
    $b = getcwd().'/';
    $c = array();
    foreach($GLOBALS['config']['plugins'][$in] as $key => $value){
        $c[$key] = $value;
    }
    $d = $in;
    eval("\$$in = new Ispweb_Plugindaemon(\"$a\",\"$b\",\$c,\"$d\");");
    echo $$in;
}
include $core_page_content;
$page_content = ob_get_contents();
ob_end_clean();

有谁知道为什么我可以访问该对象,除非我在同一个文件中调用其成员函数之一?

我能做些什么?

PS我知道设置并不理想。我对此无能为力。

谢谢!

TL; DR我正在文件 A 中创建一个对象,其中包含来自文件 B 的变量。我缓冲文件 B 以获取要馈送到文件 A 的参数,创建对象,将其打印到另一个缓冲区并将文件 B 包含在该缓冲区中也是。如果文件 B 对可能创建的对象进行了函数调用,我会收到一个致命错误:调用非对象的成员函数。

补充笔记:

文件 B:

$config['plugins']['BannerRotate'] = array(
    'container'    =>    'abroadView',
    'arrows'       =>    true
);
// page content (XHTML)

档案一:

ob_start();
$core_page_content = 'file_b';
include $core_page_content;
if(isset($config['plugins'])){
foreach($config['plugins'] as $plugin => $ary){
    $ins[] = $plugin;
}
ob_end_clean();
foreach($ins as $in){
    $$in = new Ispweb_Plugindaemon(CURRENTSRV,getcwd().'/',$config['plugins'][$in],$in);
}
include $core_page_content;
$page_content = ob_get_contents();
ob_end_clean();

// later on in the file
include 'top.htm';
include $page_content;
include 'bot.htm';
4

1 回答 1

0

问题是变量范围。

于 2011-07-18T16:43:17.487 回答