doufa5001
2017-01-05 15:12
浏览 128
已采纳

PHP使用变量来调用命名空间

Is it possible to call a namespace using a variable?

For example:

$class_name     = strtolower( $interface_name );
$return['html'] = \interfaces\$class_name::get_loginForm();

However, this returns the error:

Parse error: syntax error, unexpected '$class_name' (T_VARIABLE), expecting identifier (T_STRING)

I am connecting to several 3rd party API's which all do the same thing, but in their own unique way. The user will already be connected to their preferred API before joining our site. The middleware for the different API's will be stored within bespoke files under the same namespace.

It would be possible to solve the problem running a switch on the interface name and calling the relevant namespace, however that would mean having to locate and add to the switch(es) every time a new API comes out so any help with this problem would be appreciated. Cheers.

图片转代码服务由CSDN问答提供 功能建议

是否可以使用变量调用命名空间?

例如 :

  $ class_name = strtolower($ interface_name); 
 $ return ['html'] = \ interfaces \ $ class_name :: get_loginForm(); 
   
 
 

但是,这会返回错误:

解析错误:语法错误,意外'$ class_name'(T_VARIABLE), 期待标识符(T_STRING)

我连接到几个第三方API,它们都做同样的事情,但是以他们自己独特的方式。 在加入我们的网站之前,用户已经连接到他们的首选API。 不同API的中间件将存储在同一命名空间下的定制文件中。

可以解决在接口名称上运行交换机并调用相关命名空间的问题,但是 这意味着每次新的API发布时都必须找到并添加到交换机,所以任何有关此问题的帮助都将受到赞赏。 干杯。

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题