将现有的PHP文件和函数包含到Slim中会导致返回空变量


Include existing PHP files and functions into Slim results in empty variables being passed back

我开始将Slim用于REST/Json web服务。现在,经过初步测试,我意识到我需要包含用于"正常"web应用程序的PHP代码,以便计算要返回的正确值。也就是说,我不返回国家统计数据,而是计算人均数据或索引值或汇总。]

现在,我的PHP代码在包含的文件中(见下文,B)调用另一个文件(C)中的一个函数。这很好用。但是,在该函数中生成的值没有被带回第一个文件(B)。

  • A: 代码精简级别。。。。include(xx.php,yy.php)
  • B: xx.php:call_to_function_from_yy()
  • C: yy.php:生成结果;全局$results($results确实具有值)
  • B: $results为空

有什么提示吗?我能做些什么?非常感谢!

假设你的yy.php有这样的东西:

function returnResults($parameter) {
 /*
  * your pretty code here
  */
 return $results;
}

你的xx.php文件是这样做的:

require_once $path_to_your_file . '/yy.php';
 /*
  * your pretty code here
  */
$myResults = returnResults($myParameter);
 /*
  * your pretty code here
  */

通过这种方式,你可以在xx.php中从yy.php中获得结果,如果你能找到yy.php和xx.php的小片段,我可以改进这个答案。