20, "title" => "Something", "value" => "Som-6ren">
gpt4 book ai didi

php - 在php中的std对象中添加方法

转载 作者:IT王子 更新时间:2023-10-29 00:16:41 26 4
gpt4 key购买 nike

是否可以通过这种方式添加一个方法/函数,比如

$arr = array(
"nid"=> 20,
"title" => "Something",
"value" => "Something else",
"my_method" => function($arg){....}
);

或者像这样

$node = (object) $arr;
$node->my_method=function($arg){...};

如果可能的话,我该如何使用该函数/方法?

最佳答案

现在可以在 PHP 7.1 中使用匿名类实现

$node = new class {
public $property;

public function myMethod($arg) {
...
}
};

// and access them,
$node->property;
$node->myMethod('arg');

关于php - 在php中的std对象中添加方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11503212/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com