溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

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

發布時間:2021-03-10 14:39:55 來源:億速云 閱讀:265 作者: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

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女