2014-10-16 14:59
浏览 232


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' => '

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条回答 默认 最新

  • dongyuan9109 2014-10-16 15:10

    Check out this post. How can I call a static method on a variable class?

    It look like your code is ok in php 5.3. There's also some ideas how to deal with your problem if you are < 5.3.

    点赞 评论

相关推荐 更多相似问题