Home  >  Article  >  Backend Development  >  Example of dynamically calling class static method using php variable class name

Example of dynamically calling class static method using php variable class name

WBOY
WBOYOriginal
2016-07-25 08:52:423665browse
  1. class cls
  2. {
  3. public static function test($userName)
  4. {
  5. echo 'Hi, ' . $userName . "\n";
  6. }
  7. }
  8. $className = 'cls';
  9. $className::test('Tom'); // PHP >= 5.3.0
  10. call_user_func(array($className, 'test'), 'Jack'); // PHP 3 >= 3.0.3, PHP 4, PHP 5
  11. call_user_func_array(array($className, 'test'), array('Lily')); // PHP 4 >= 4.0.4, PHP 5
复制代码

参见:http://cn.php.net/manual/zh/language.oop5.static.php

另外,也可以借助 php eval 函数来实现,这个大家自行研究下。



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn