首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

runkit_method_redefine

(PECL runkit >= 0.7.0)

runkit_method_redefine - 动态更改给定方法的代码

描述

代码语言:javascript
复制
bool runkit_method_redefine ( string $classname , string $methodname , string $args , string $code [, int $flags = RUNKIT_ACC_PUBLIC [, string $doc_comment = NULL ]] )
代码语言:javascript
复制
bool runkit_method_redefine ( string $classname , string $methodname , Closure $closure [, int $flags = RUNKIT_ACC_PUBLIC [, string $doc_comment = NULL ]] )

注意:此函数不能用于操作当前正在运行的(或链接的)方法。

参数

classname

重新定义方法的类

methodname

要重新定义的方法的名称

args

重定义方法的逗号分隔参数列表

code

methodname调用时要评估的新代码

closure

定义方法的闭包。

flags

重新定义的方法可以是RUNKIT_ACC_PUBLICRUNKIT_ACC_PROTECTEDRUNKIT_ACC_PRIVATE任选地通过位或具有组合RUNKIT_ACC_STATIC(自1.0.1)

doc_comment

函数的doc评论。

返回值

成功返回TRUE或失败时返回FALSE

更新日志

描述

runkit 1.0.4

期望封闭的另一种语法已被添加。

runkit 1.0.4

可选参数doc_comment已添加。

例子

Example #1 runkit_method_redefine() example

代码语言:javascript
复制
<?php
class Example {
    function foo() {
        return "foo!\n";
    }
}

// create an Example object
$e = new Example();

// output Example::foo() (before redefine)
echo "Before: " . $e->foo();

// Redefine the 'foo' method
runkit_method_redefine(
    'Example',
    'foo',
    '',
    'return "bar!\n";',
    RUNKIT_ACC_PUBLIC
);

// output Example::foo() (after redefine)
echo "After: " . $e->foo();
?>

上面的例子将输出:

代码语言:javascript
复制
Before: foo!
After: bar!

← runkit_method_copy

runkit_method_remove →

扫码关注腾讯云开发者

领取腾讯云代金券