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

原创
2016-06-06 20:47:30 1050浏览

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);

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

回复内容:

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);

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

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

$model = 'ArticleModel';
$call = "\\Common\\Model\\{$model}";

call_user_func_array(array($call, 'save'), array(123123));
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。