PHP Conference Japan 2024

runkit7_method_add

(PECL runkit7 >= 未知)

runkit7_method_add動態地將新方法新增至指定的類別

說明

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

參數

class_name

要新增此方法的類別名稱

method_name

要新增的方法名稱

argument_list

新建立方法的參數,以逗號分隔的列表

code

呼叫 method_name 時要執行的程式碼

closure

定義方法的 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_add() 範例

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

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

// 新增一個新的公開方法
runkit7_method_add(
'Example',
'add',
'$num1, $num2',
'return $num1 + $num2;',
RUNKIT7_ACC_PUBLIC
);

// 計算 12 + 4
echo $e->add(12, 4);
?>

上述範例將輸出

16

參見

新增筆記

使用者貢獻的筆記

此頁面沒有使用者貢獻的筆記。
To Top