Home >Backend Development >PHP Tutorial >PHP命名空间里的静态方法 能不能动态调用?

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

WBOY
WBOYOriginal
2016-06-06 20:47:301444browse

File1:

<code>// file1.php
namespace Common\Model;

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

File2:

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

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

回复内容:

File1:

<code>// file1.php
namespace Common\Model;

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

File2:

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

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

<code class="lang-php">namespace Admin\Controller;  
include('./file1.php');  
$fqcn = '\\Common\\Model\\AritcleModel';  
$func = 'save';  
$fqcn::$func(123123);  
</code>

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

call_user_func_array(array($call, 'save'), array(123123));
</code>
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