- 论坛徽章:
- 0
|
(1)。runkit_method_rename
这个函数可以动态的改变我们所调用的函数的名字。
class Test
{
function foo() {
return "foo! ";
}
}
runkit_method_rename(
'Test', //类名
'foo',//实际调用的函数
'bar'//显示调用的函数
);
echo Test::bar();
程序将输出
foo!
(2) runkit_method_add
这个函数可以动态的向类中添加函数
class Test
{
function foo() {
return "foo! ";
}
}
runkit_method_add(
Test, //类名
'add', //新函数名
'$num1, $num2',//传入参数
'return $num1 + $num2;',//执行的代码
RUNKIT_ACC_PUBLIC
);
// 调用
echo $e->add(12, 4);
(3)runkit_method_copy
可以把A类中的函数拷贝到类B中并对函数重命名
class Foo {
function example() {
return "foo! ";
}
}
class Bar {
//空类
}
//执行拷贝
runkit_method_copy('Bar', 'baz', 'Foo', 'example');
//执行拷贝后的函数
echo Bar::baz();
(4) runkit_method_redefine
动态的修改函数的返回值
这个函数可以让我们轻松的实现对类的MOCK测试!是不是很COOL呢
class Example {
function foo() {
return "foo! ";
}
}
//创建一个测试对象
$e = new Example();
// 在测试对象之前输出
echo "Before: " . $e->foo();
// 修改返回值
runkit_method_redefine(
'Example',
'foo',
'',
'return "bar! ";',
RUNKIT_ACC_PUBLIC
);
// 执行输出
echo "After: " . $e->foo();
(5)runkit_method_remove
这个函数就很简单了,看名字就能看出来了,动态的从类中移除函数
class Test {
function foo() {
return "foo! ";
}
function bar() {
return "bar! ";
}
}
// 移除foo函数
runkit_method_remove(
'Test',
'foo'
);
echo implode(' ', get_class_methods('Test'));
程序输出
bar
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u1/41952/showart_2043652.html |
|