dqhnp44220 2014-10-16 14:59
浏览 251
已采纳

PHP按类名调用类的静态方法

This is a syntax question I think... I have an array of classnames, that I use in a factory to generate objects by object type code:

$array = ['a' => '
amespace\AClass', 'b' => '
amespace\BClass'];

I can instantiate these classes from the string name just fine:

$classname = $array['a'];
return new $classname($arg1, $arg2);

What I am trying to do is call a static method of the class named in the array or string, without having to initialize the object - something like:

$classname = $array['a'];
return $classname::formatArg($arg1);

Obviously, this doesn't work since $classname is a string, so how do I tell PHP I am trying to access the object with that name?

  • 写回答

1条回答

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 vue3为什么请求在这边获取不到数据呢(关键词-select)
      • ¥20 Multisim仿真设计三路智力抢答装置
      • ¥15 用UML制作简单的语音计算机
      • ¥20 用socket实现ping遇到的问题
      • ¥15 综合的类,这个类里面里面又有构造方法,重载方法,有继承,有重写,有接口
      • ¥15 电脑重置失败无法开机也进不了修复模式,如何解决?
      • ¥100 px4飞控如何设置平飞模式下飞机固定的仰角
      • ¥15 关于#python#的问题:不可以使用max ,min ,exit ,break 的情况下找出哪一年份出生率最高,以及哪一年份出生率最低
      • ¥15 WPF中使用DoDragDrop程序会崩,"FatalExecutionEngineError"
      • ¥15 C语言 堆排序 数据结构