怎么在php中利用反射调用私人的方法

  介绍

今天就跟大家聊聊有关怎么在php中利用反射调用私人方法,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

<强>简单被测试类

生成一个简单的被测试类,只有个私人方法。

代码如下:


& lt; ?php/* * *崔小涣单测的基本模板。* * @author cuihuan * @date 2015/11/12 22:15:31 * @version修订美元:1.0美元* */MyClass类{/* * *私有方法* * @param params * @return bool */私有函数privateFunc美元($ params){如果(!收取(params)美元){返回false;}回声“测试success";返回params美元;}}

<强>单测代码

代码如下:


& lt; ?php/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * $ Id: MyClassTest T v 1.0 PsCaseTest cuihuan Exp $ * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *//* * *崔小涣单测的基本模板。* * @author cuihuan * @date 2015/11/12 22:09:31 * @version修订美元:1.0美元* */require_once (& # 39;。/MyClass.php& # 39;);类MyClassTest延伸PHPUnit_Framework_TestCase {const CLASS_NAME=& # 39; myclass # 39;; const FAIL =& # 39;失败# 39;;保护objMyClass;美元/* * * @brief设置:设置固定装置,例如,打开网络连接。* *可以看做phpunit)的构造函数*/公共职能的设置(){作用(& # 39;中华人民共和国# 39;);$ this→objMyClass=new MyClass();}/* * *利用反射,对类中的私有和保护方法进行单元测试* * @param $ strMethodName字符串:反射函数名* @return ReflectionMethod obj :回调对象*/保护静态函数getPrivateMethod (strMethodName美元){$ objReflectClass=新ReflectionClass (self:: CLASS_NAME);方法=objReflectClass→美元getMethod ($ strMethodName);美元方法→种setAccessible(真正);回归方法;美元}/* * * @brief:测试私人函数的调用*/公共职能testPrivateFunc () {$ testCase=& # 39;只是一个测试字符串# 39;;//反射该类testFunc美元=self:: getPrivateMethod (& # 39; privateFunc& # 39;); res=testFunc→美元invokeArgs ($ this→objMyClass,阵列(testCase美元));$ this→assertequal (testCase, res美元);$ this→expectOutputRegex(& # 39;/成功/我# 39;);//捕获没有参数异常测试一试{testFunc→美元invokeArgs ($ this→transfer2Pscase,数组());}捕捉(异常预期美元){$ this→assertNotNull(预期美元),返回true;} $ this→失败(self::失败);}}

<>强运行结果

cuihuan:测试cuixiaohuan phpunit)美元MyClassTest。php PHPUnit) 4.8.6塞巴斯蒂安·伯格曼和贡献者。时间:103 ms,记忆:11.75 mbok(1测试,3断言)

<强>关键代码分析

封装了一个,被测类方法的反射调用;同时,返回方法之前处理方法的接入权限为真,便可以访问私人的函数方法。

代码如下:


/* * *利用反射,对类中的私有和保护方法进行单元测试* * @param $ strMethodName字符串:反射函数名* @return ReflectionMethod obj :回调对象*/保护静态函数getPrivateMethod (strMethodName美元){$ objReflectClass=新ReflectionClass (self:: CLASS_NAME);方法=objReflectClass→美元getMethod ($ strMethodName);美元方法→种setAccessible(真正);回归方法;美元}

<强>下面给大家分享java中利用反射调用另一类的私有方法

我们知道,java应用程序不能访问持久化类的私有方法,但Hibernate没有这个限制,它能够访问各种级别的方法,如私人的,默认情况下,保护,公众。Hibernate是如何实现该功能的呢?答案是利用JAVA的反射机制,如下:,

& lt; span 祝辞import  java.lang.reflect.InvocationTargetException;,   import  java.lang.reflect.Method,   {public  class  ReflectDemo    ,public  static  void  main (String [], args), throws  Exception  {,   Method 才能;Method =, PackageClazz.class.getDeclaredMethod (“privilegedMethod",, new 类[]{String.class, String.class});,,   method.setAccessible才能(真正的),,   method.invoke才能(new  PackageClazz (),“452345234”、“q31234132"),,   }大敌;   },   {class  PackageClazz    ,private  void  privilegedMethod (String  invokerName String  adb), {,   System.out.println才能(“——产生绯闻;+ invokerName +“- - - - -“+亚行),,   }大敌;   }& lt;/span>

输出结果为:——-452345234——q31234132

看完上述内容,你们对怎么在php中利用反射调用私人方法有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注行业资讯频道,感谢大家的支持。

怎么在php中利用反射调用私人的方法