Home  >  Article  >  Backend Development  >  Detailed explanation of PHP reflection method calling private methods in execution class

Detailed explanation of PHP reflection method calling private methods in execution class

黄舟
黄舟Original
2017-08-17 13:57:532588browse

In our daily development work, we often use private methods in calling classes. Today I will introduce to you how PHP calls private methods in execution classes through reflection methods. I hope this tutorial can help all friends who are interested. You can come in and take a look. Features a full reflection API, adding the ability to reverse engineer classes, interfaces, functions, methods, and extensions.


#The first step, first Download the PHP reflection method we need to use in this course to call the private method library in the execution class: http://www.php.cn/xiazai/leiku/594

Step 2: After the download is completed Find the php class file we need, unzip it to our local directory, and create a new php file!

The third step, after completion, we need to call this class in the new php file and instantiate the class:

<?php
include_once "myclass.php";//引入类文件
//通过类名MyClass进行反射
$ref_class = new ReflectionClass(&#39;MyClass&#39;);

//通过反射类进行实例化
$instance  = $ref_class->newInstance();

//通过方法名myFun获取指定方法
$method = $ref_class->getmethod(&#39;myFun&#39;);

//设置可访问性
$method->setAccessible(true);

//执行方法
$method->invoke($instance);


?>


Run the file and the result will be as shown below:

Detailed explanation of PHP reflection method calling private methods in execution class

The above is the detailed content of Detailed explanation of PHP reflection method calling private methods in execution class. For more information, please follow other related articles on the PHP Chinese website!

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