这样也可以达到反射的效果,该如何处理

WBOY
Release: 2016-06-13 13:41:54
Original
877 people have browsed it

这样也可以达到反射的效果

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php class UserClass{
    public function userMethod($userParameter='default'){
        echo 'invoke success!!!';
    }
}
/****************************反射*********************************/
$className ='UserClass';
$functionName='userMethod';
    $reflection = new ReflectionClass($className);
    if($reflection->isUserDefined()){
        if($reflection->hasMethod($functionName)){
            $method = $reflection->getMethod($functionName);
            if($method->isStatic()){
                $method->invoke(null);
            }else{
                $instance = $reflection->newInstance();
                $method->invoke($instance);
            }
        }
    }
/****************************反射*********************************/
/*****************************************************************/
$user=new $className();
$user->$functionName();
/*****************************************************************/
?>
Copy after login

类名可以从配置文件中动态加载出来,然后创建相应类型的实例。效果是一样的,用反射反而觉得有点麻烦。个人意见,请大家批评指正。

------解决方案--------------------
恩。学习了。
java的反射机制
http://baike.baidu.com/view/1865203.htm

其实想想“反射机制”的目的,可能会对php抱怨就不会太多了
------解决方案--------------------
学习,第一次了解 反射
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!