PHP中具有名称空间的变量函数 [英] Variable functions with namespaces in PHP

查看:65
本文介绍了PHP中具有名称空间的变量函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否存在一种使用命名空间调用变量函数的方法.基本上,我试图解析标签并将其发送给模板函数,以便它们可以呈现html`

I'm wondering if there is a way to call variable functions with namespaces. Basically I'm trying to parse tags and send them to template functions so they can render html`

这是一个示例:(我使用的是PHP 5.3)

Here's an Example: (I'm using PHP 5.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,预计在第76行的...中出现T_STRING

I keep getting Parse error: syntax error, unexpected T_VARIABLE, expecting T_STRING in ... on line 76

谢谢!马特

推荐答案

可以,但是很遗憾,您需要使用

Sure you can, but unfortunately, you need to use call_user_func() to achieve this:

require_once 'template.php';
foreach (array("javascript","script","css") as $tag) {
    echo call_user_func('template\\'.$tag);
}

PHP中的命名空间是相当新的.我确定将来会解决此问题,因此我们将不需要 call_user_func() 了.

Namespaces in PHP are fairly new. I'm sure that in the future, they will fix it so we won't require call_user_func() anymore.

这篇关于PHP中具有名称空间的变量函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆