Home Backend Development PHP Tutorial 这样也可以达到反射的效果,该如何处理

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

Jun 13, 2016 am 10:11 AM
gt method reflection

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

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?phpclass UserClass{    public function userMethod($userParameter='default'){        echo 'invoke success!!!';    }}/****************************反射*********************************/$className ='UserClass';$functionName='userMethod';    $reflection = new ReflectionClass($className);    if($reflection->isUserDefined()){        if($reflection-&gt;hasMethod($functionName)){            $method = $reflection-&gt;getMethod($functionName);            if($method-&gt;isStatic()){                $method-&gt;invoke(null);            }else{                $instance = $reflection-&gt;newInstance();                $method-&gt;invoke($instance);            }        }    }/****************************反射*********************************//*****************************************************************/$user=new $className();$user-&gt;$functionName();/*****************************************************************/?&gt;
Copy after login

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

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

其实想想“反射机制”的目的,可能会对php抱怨就不会太多了
------解决方案--------------------
学习,第一次了解 反射
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

Hot Article

Hot Article

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

What are the differences between Huawei GT3 Pro and GT4?

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Fix: Snipping tool not working in Windows 11

How to analyze CLDAP protocol Reflection DDoS How to analyze CLDAP protocol Reflection DDoS May 22, 2023 pm 01:13 PM

How to analyze CLDAP protocol Reflection DDoS

Solution to PHP Fatal error: Call to a member function fetch() Solution to PHP Fatal error: Call to a member function fetch() Jun 23, 2023 am 09:36 AM

Solution to PHP Fatal error: Call to a member function fetch()

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

How to Fix Can't Connect to App Store Error on iPhone

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决

Is watch4pro better or gt? Is watch4pro better or gt? Sep 26, 2023 pm 02:45 PM

Is watch4pro better or gt?

How to use POST request method in jQuery How to use POST request method in jQuery Feb 28, 2024 pm 09:03 PM

How to use POST request method in jQuery

See all articles