㈠ php的类的private变量调用问题
因为__get(); __set() 方法在类的内部,类的内部是可以调用类的私有属性的变量
㈡ 反射调用private方法实践(php、java)
单测中有个普遍性的问题,被侧类中的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
style="font-size:14px;">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 static 方法名 是什么意思
private 关键字声名 一个私有的方法或属性;定义了private的方法或属性在类的外部不能访问,只能在本类中使用。
类中使用私有方法:
$this->属性 或 方法()
static 关键字 是创建一个静态方法或静态属性;定义了static 的方法或属性不需要实例化就可以使用。
访问静态变量 类名::$变量名;
访问静态方法 类名::方法名();
访问本类静态变量 self::$变量名;
访问本类静态方法 self::方法名();
访问父类静态变量 parent::$变量名;
访问父类静态方法 parent::方法名();
㈣ php如何通过子类继承父类共有方法访问父类私有属性
在父类中加个public方法,
如private
$aaa
=
'test';
public
function
getAAA(){
return
$this->aaa;
}
在子类中调用parent::getAAA();就可以获得父类私有属性
当然如果你想获取多个,可以改成动态变量名,
getAAA($v){
return
$this->{$v};
}
㈤ php子类可以继承和访问父类的私有属性和方法吗
你这样理解也可以,当父类有私有的方法和属性,子类是继承不到的,所以子类不能访问父类的私有方法和属性。java
特性,private:类中只要限定为private的成员,只能被这个类本身访问
㈥ PHP怎么调用其他类的方法
在Java的调用方法是import,而在PHP中没有import这个函数,一般PHP中调用其他类是用到require(),具体PHP调用其他类的方法如下:
1、首先应该先有一个文件名为tool.php的文件,在文件中声明一个类。
(6)php调用私有方法扩展阅读:
类是变量与作用于这些变量的函数的集合。使用下面的语法定义一个类:
<?php
class Cart { var $items; // 购物车中的物品
// 将 $num 个 $artnr 物品加入购物车
function add_item($artnr, $num) {
$this->items[$artnr] += $num;
} // 将 $num 个 $artnr 物品从购物车中取出
function remove_item($artnr, $num) {
if ($this->items[$artnr] > $num) {
$this->items[$artnr] -= $num; return true;
} elseif
($this->items[$artnr] == $num) {
unset($this->items[$artnr]); return true;
} else {
return false; }
}
} ?>
上面的例子定义了一个 Cart 类,这个类由购物车中的商品构成的数组和两个用于从购物车中添加和删除商品的函数组成。
㈦ [php]private public protected 三者没弄明白
public,公开的,是所有实例都可以访问
protected,受保护的,只有存在继承关系的可以访问,比如子类,父类
private,私有的,只有对应的类内部能够调用