我可以在drupal中使用静态方法作为菜单回调吗?

All*_*nde 6 drupal drupal-6

在定义hook_menu项目时,我可以在类上使用公共静态方法而不是使用drupal倾向于使用的全局下划线命名约定吗?

例如,以下是否可以接受?

$items['test'] = array(
  'page callback' => 'MyClass::test',
  'access arguments' => array('access content'),
  'type' => MENU_CALLBACK
);
Run Code Online (Sandbox Code Playgroud)

kia*_*uno 2

menu_execute_active_handler()是调用菜单回调的 Drupal 函数,包含以下代码:

if ($router_item = menu_get_item($path)) {
  if ($router_item['access']) {
    if ($router_item['file']) {
      require_once($router_item['file']);
    }
    return call_user_func_array($router_item['page_callback'], $router_item['page_arguments']);
  }
  else {
    return MENU_ACCESS_DENIED;
  }
}
Run Code Online (Sandbox Code Playgroud)

在 PHP 5.2.3 或更高版本中,可以调用call_user_func()as call_user_func('MyClass::myCallbackMethod')

我能看到的唯一问题是第三方模块不期望菜单回调是类静态方法,并使用function_exists($menu_callback).
然后,正如 Coder1 报告的那样,如果 Drupal 核心模块或其他模块尝试使用类似于以下的代码调用 menu_callback,那么它们可能会导致 PHP 错误。

$menu_callback = $router_item['page_callback'];
$menu_callback($router_item['page_arguments']);
Run Code Online (Sandbox Code Playgroud)