代码之家  ›  专栏  ›  技术社区  ›  Palantir

在名为的类上调用静态方法?

php
  •  1
  • Palantir  · 技术社区  · 16 年前

    有没有办法在PHP版本中获得这种效果 先前的 到5.3?

    class A {
      public static function x() {print 'ok'; }
    }
    
    $className = 'A';
    $className::x();
    

    谢谢! 真知晶球

    3 回复  |  直到 16 年前
        1
  •  3
  •   Romain Deveaud    16 年前

    是的,您可以使用call_user_func_array:

    call_user_func_array(array($className, 'x'), array()) ;
    
        2
  •  2
  •   Mark Baker    16 年前
    $className = 'A'; 
    call_user_func(array($className,'x'));
    
        3
  •  1
  •   nothrow    16 年前
    call_user_func(array($className,'x'));
    

    看见 http://cz.php.net/manual/en/function.call-user-func.php

    推荐文章