是否可以通过这种方式添加一个方法/函数,比如
$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/