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

WBOY
Release: 2016-07-25 08:52:42
Original
3688 people have browsed it
  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 函数来实现,这个大家自行研究下。



source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!