Переменные функции с пространствами имен в PHP

Если мы говорим [приблизительно 111] Запрос заголовки, можно создать собственные заголовки при выполнении XmlHttpRequests.

var request = new XMLHttpRequest();
request.setRequestHeader("X-Requested-With", "XMLHttpRequest");
request.open("GET", path, true);
request.send(null);
11
задан Matt 9 August 2009 в 01:10
поделиться

3 ответа

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);
}

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.

5
ответ дан 3 December 2019 в 08:56
поделиться

Это также будет работать, нет необходимости в call_user_func , просто используйте Variable functions Docs feature:

require_once 'template.php';

$ns = 'template';
foreach (array('javascript', 'script', 'css') as $tag) {
    $ns_func = $ns . '\\' . $tag;
    echo $ns_func();
}
7
ответ дан 3 December 2019 в 08:56
поделиться

try with

 // 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"; }

you have some info here

1
ответ дан 3 December 2019 в 08:56
поделиться
Другие вопросы по тегам:

Похожие вопросы: