我想知道是否有一种方法可以调用带有名称空间的变量函数。基本上,我尝试解析标签并将它们发送到模板函数,这样它们就可以呈现html`
下面是一个例子:(我使用的是PHP5.3)
// Main php file
require_once 'template.php';
foreach (array("javascript","script","css") as $tag) {
echo template\$tag();
}
// template.php
namespace template;
function javascript() { return "Hello from javascript"; }
function css() { return "Hello from css"; }
function script() { return "Hello from script"; }
我一直收到解析错误:语法错误,意外的T_VARIABLE,期望的T_STRING在...在第76行
谢谢!哑光
发布于 2009-08-08 17:14:43
当然可以,但不幸的是,您需要使用call_user_func()
来实现这一点:
require_once 'template.php';
foreach (array("javascript","script","css") as $tag) {
echo call_user_func('template\\'.$tag);
}
PHP中的名称空间是相当新的。我相信在未来,他们会修复它,这样我们就不再需要call_user_func()
了。
发布于 2009-08-08 17:14:20
尝试使用
// Main php file
require_once 'template.php';
foreach (array("javascript","script","css") as $tag) {
call_user_func("template\\$tag"); // As of PHP 5.3.0
}
// template.php
namespace template;
function javascript() { return "Hello from javascript"; }
function css() { return "Hello from css"; }
function script() { return "Hello from script"; }
你有一些信息here
发布于 2012-03-12 20:07:20
尝尝这个
$p = 'login';
namespace App\login;
$test2 = '\App\\'.$p.'\\MyClass';
$test = new $test2;
https://stackoverflow.com/questions/1250297
复制相似问题