PHP8 runkit7_method_add — 動(dòng)態(tài)地向給定類添加一個(gè)新方法

2023-10-07 10:52 更新

(PECL runkit7 >= Unknown)

runkit7_method_add — 動(dòng)態(tài)地向給定類添加一個(gè)新方法。

說(shuō)明

runkit7_method_add(
    string $class_name,
    string $method_name,
    string $argument_list,
    string $code,
    int $flags = RUNKIT7_ACC_PUBLIC,
    string $doc_comment = null,
    string $return_type = ?,
    bool $is_strict = ?
): bool
runkit7_method_add(
    string $class_name,
    string $method_name,
    Closure $closure,
    int $flags = RUNKIT7_ACC_PUBLIC,
    string $doc_comment = null,
    string $return_type = ?,
    bool $is_strict = ?
): bool

參數(shù)

class_name

要向其添加新方法的類

method_name

要添加的方法的名稱

argument_list

逗號(hào)分隔的參數(shù)列表,用于新創(chuàng)建的方法

code

方法被調(diào)用時(shí)要評(píng)估的代碼

closure

定義方法的閉包

flags

要?jiǎng)?chuàng)建的方法類型,可以是RUNKIT7_ACC_PUBLIC、RUNKIT7_ACC_PROTECTED或RUNKIT7_ACC_PRIVATE,可以選擇通過(guò)位運(yùn)算與RUNKIT7_ACC_STATIC組合使用

doc_comment

方法的文檔注釋

return_type

方法的返回類型

is_strict

方法是否表現(xiàn)得像是在啟用 ?strict_types=1? 的文件中聲明的

返回值

成功時(shí)返回 true, 或者在失敗時(shí)返回 false。

示例

示例 #1 runkit7_method_add() example

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

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

// Add a new public method
runkit7_method_add(
'Example',
'add',
'$num1, $num2',
'return $num1 + $num2;',
RUNKIT7_ACC_PUBLIC
);

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

以上示例會(huì)輸出:

16

參見(jiàn)

  • runkit7_method_copy() - 從一個(gè)類復(fù)制方法到另一個(gè)類 
  • runkit7_method_redefine() - 動(dòng)態(tài)更改給定方法的代碼 
  • runkit7_method_remove() - 動(dòng)態(tài)移除給定方法 
  • runkit7_method_rename() - 動(dòng)態(tài)更改給定方法的名稱 
  • runkit7_function_add() - 添加一個(gè)新的函數(shù),類似于 create_function


以上內(nèi)容是否對(duì)您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)