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

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

WBOY
Release: 2016-06-06 20:47:30
Original
1443 people have browsed it

File1:

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

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

File2:

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

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

回复内容:

File1:

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

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

File2:

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

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

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

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

call_user_func_array(array($call, 'save'), array(123123));
</code>
Copy after login
Related labels:
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