search

Home  >  Q&A  >  body text

PHP命名空间里的静态方法 能不能动态调用?

File1:

// file1.php
namespace Common\Model;

class ArticleModel {
    static function save($id=0) {
        die('xxx');
    }
}

File2:

// file2
namespace Admin\Controller;
include('./file1.php');
$model = 'ArticleModel';
$call = "\\Common\\Model\\$model::save";
$call(123123);

这种方法 我试了,不能调用。
是不是静态方法 不支持动态调用?
还是有别的方式可以调用?

大家讲道理大家讲道理2901 days ago308

reply all(2)I'll reply

  • 巴扎黑

    巴扎黑2017-04-10 14:41:01

    namespace Admin\Controller;  
    include('./file1.php');  
    $fqcn = '\\Common\\Model\\AritcleModel';  
    $func = 'save';  
    $fqcn::$func(123123);  
    

    reply
    0
  • PHPz

    PHPz2017-04-10 14:41:01

    $model = 'ArticleModel';
    $call = "\\Common\\Model\\{$model}";
    
    call_user_func_array(array($call, 'save'), array(123123));
    

    reply
    0
  • Cancelreply