【发布时间】:2014-12-02 11:17:13
【问题描述】:
我正在尝试在 Zend2 MVC 框架中封装一个遗留应用程序。感谢Zend Skeleton Application 和代码示例(尤其是https://github.com/maglnet/MaglLegacyApplication),我已经解决了大部分问题。
以下“遗留”文件说明了我无法解决的一个大问题:
<?php
$test = "test";
function echo_test(){
global $test;
echo "test = ";
var_dump($test); # Makes NULL explicit
}
echo_test();
在 ZF2 模块的控制器中,我使用输出缓冲区捕获包含的输出并将其粘贴到响应对象中:
...
chdir($filePath) # Fixes relative includes
ob_start();
include $filePathAndName;
$output = ob_get_clean();
$response->setContent($output);
return $response;
...我回复test = NULL。
我看到了有关 ZF2 命名空间可能会为遗留文件带来问题的警告,并已尽我所能尝试阐明原因。根据 PHP 指南,"Without any namespace definition, all class and function definitions are placed into the global space"。确实,我的示例仅比该语句下方列出的示例稍微复杂...但它似乎不起作用。
我继续研究,终于发现这种方法“will import the contents of the include file into the method scope, not the class scope”。
有没有办法在方法范围之外处理文件?
【问题讨论】:
标签: php namespaces zend-framework2 global-variables legacy