温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

怎么在php中利用反射调用private方法

发布时间:2021-03-10 14:39:55 来源:亿速云 阅读:209 作者:Leah 栏目:开发技术

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

简单被测试类

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

复制代码 代码如下:


<?php/** * 崔小涣单测的基本模板。 * * @author cuihuan * @date 2015/11/12 22:15:31 * @version $Revision:1.0$ **/class MyClass {/** * 私有方法 * * @param $params * @return bool */private function privateFunc($params){if(!isset($params)){return false;}echo "test success";return $params;}}

单测代码

复制代码 代码如下:


<?php/*************************************************************************** * * $Id: MyClassTest T,v 1.0 PsCaseTest cuihuan Exp$ * **************************************************************************//** * 崔小涣单测的基本模板。 * * @author cuihuan * @date 2015/11/12 22:09:31 * @version $Revision:1.0$ **/require_once ('./MyClass.php');class MyClassTest extends PHPUnit_Framework_TestCase {const CLASS_NAME = 'MyClass';const FAIL  = 'fail';protected $objMyClass;/** * @brief setup: Sets up the fixture, for example, opens a network connection. * * 可以看做phpunit的构造函数 */public function setup() {date_default_timezone_set('PRC');$this->objMyClass = new MyClass();}/** * 利用反射,对类中的private 和 protect 方法进行单元测试 * * @param $strMethodName string :反射函数名 * @return ReflectionMethod obj  :回调对象 */protected static function getPrivateMethod($strMethodName) {$objReflectClass = new ReflectionClass(self::CLASS_NAME);$method = $objReflectClass->getMethod($strMethodName);$method->setAccessible(true);return $method;}/** * @brief :测试private函数的调用 */public function testPrivateFunc(){$testCase = 'just a test string';// 反射该类$testFunc = self::getPrivateMethod('privateFunc');$res = $testFunc->invokeArgs($this->objMyClass, array($testCase));$this->assertEquals($testCase, $res);$this->expectOutputRegex('/success/i');// 捕获没有参数异常测试try { $testFunc->invokeArgs($this->transfer2Pscase, array());} catch (Exception $expected) {$this->assertNotNull($expected);return true;}$this->fail(self::FAIL);}}

运行结果

cuihuan:test cuixiaohuan$ phpunit MyClassTest.php PHPUnit 4.8.6 by Sebastian Bergmann and contributors.Time: 103 ms, Memory: 11.75MbOK (1 test, 3 assertions)

关键代码分析

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

复制代码 代码如下:


/** * 利用反射,对类中的private 和 protect 方法进行单元测试 * * @param $strMethodName string :反射函数名 * @return ReflectionMethod obj  :回调对象 */protected static function getPrivateMethod($strMethodName) {$objReflectClass = new ReflectionClass(self::CLASS_NAME);$method = $objReflectClass->getMethod($strMethodName);$method->setAccessible(true);return $method;}

下面给大家分享java中利用反射调用另一类的private方法

我们知道,Java应用程序不能访问持久化类的private方法,但Hibernate没有这个限制,它能够访问各种级别的方法,如private, default, protected, public. Hibernate是如何实现该功能的呢?答案是利用JAVA的反射机制,如下: 

<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 Class[]{String.class,String.class});  
  method.setAccessible(true); 
  method.invoke(new PackageClazz(), "452345234","q31234132"); 
 } 
} 
class PackageClazz { 
 private void privilegedMethod(String invokerName,String adb) { 
  System.out.println("---"+invokerName+"----"+adb); 
 } 
}</span>

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

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

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI