PHP Conference Japan 2024

runkit7_method_redefine

(PECL runkit7 >= 未知)

runkit7_method_redefine動態更改指定方法的程式碼

說明

runkit7_method_redefine(
    字串 $class_name,
    字串 $method_name,
    字串 $argument_list,
    字串 $code,
    整數 $flags = RUNKIT7_ACC_PUBLIC,
    字串 $doc_comment = null,
    字串 $return_type = ?,
    布林值 $is_strict = ?
): 布林值
runkit7_method_redefine(
    字串 $class_name,
    字串 $method_name,
    閉包 $closure,
    整數 $flags = RUNKIT7_ACC_PUBLIC,
    字串 $doc_comment = null,
    字串 $return_type = ?,
    布林值 $is_strict = ?
): 布林值

參數

class_name

要重新定義方法的類別

method_name

要重新定義的方法的名稱

argument_list

重新定義方法的參數,以逗號分隔

code

呼叫 method_name 時要執行的程式碼

closure

定義方法的 閉包

flags

重新定義的方法可以是 RUNKIT7_ACC_PUBLICRUNKIT7_ACC_PROTECTEDRUNKIT7_ACC_PRIVATE,並可選擇透過位元 OR 與 RUNKIT7_ACC_STATIC 組合

doc_comment

方法的說明文件註釋。

return_type

方法的回傳型別。

is_strict

方法是否如同在 strict_types=1 的檔案中宣告一樣。

回傳值

成功時回傳 true,失敗時回傳 false

範例

範例 #1 runkit7_method_redefine() 範例

<?php
class Example {
function
foo() {
return
"foo!\n";
}
}

// 建立一個 Example 物件
$e = new Example();

// 輸出 Example::foo() (重新定義前)
echo "Before: " . $e->foo();

// 重新定義 'foo' 方法
runkit7_method_redefine(
'Example',
'foo',
'',
'return "bar!\n";',
RUNKIT7_ACC_PUBLIC
);

// 輸出 Example::foo() (重新定義後)
echo "After: " . $e->foo();
?>

上述範例將輸出:

Before: foo!
After: bar!

參見

新增註解

使用者貢獻的註解

此頁面沒有使用者貢獻的註解。
To Top