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


Add method in an std object in php

是否可以像一样以这种方式添加方法/函数

$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');

您不能将方法动态添加到stdClass并以正常方式执行它。然而,你可以做一些事情。

在第一个示例中,您正在创建一个闭包。您可以通过发出以下命令来执行该关闭:

$arr['my_method']('Argument')

您可以创建一个stdClass对象并为其属性之一分配闭包,但由于语法冲突,您无法直接执行它

$node = new stdClass();
$node->method = function($arg) { ... }
$func = $node->method;
$func('Argument');

正在尝试

$node->method('Argument')

会生成错误,因为stdClass上不存在方法"method"。

请参阅这个SO答案,了解使用魔术方法__call的一些巧妙技巧。

由于PHP 7,还可以直接调用匿名函数属性:

$obj = new stdClass;
$obj->printMessage = function($message) { echo $message . "'n"; };
echo ($obj->printMessage)('Hello World'); // Hello World

这里表达式$obj->printMessage产生匿名函数,该匿名函数然后直接用参数'Hello World'执行。然而,在调用函数表达式之前,有必要将其放入paranethes中,这样以下操作仍然会失败:

echo $obj->printMessage('Hello World'); 
// Fatal error: Uncaught Error: Call to undefined method stdClass::printMessage()

另一个解决方案是创建一个匿名类,并通过神奇的函数__call代理调用,使用箭头函数,您甚至可以引用上下文变量:

 new Class ((new ReflectionClass("MyClass"))->getProperty("myProperty")) {
            public function __construct(ReflectionProperty $ref)
            {
                $this->setAccessible = fn($o) => $ref->setAccessible($o);
                $this->isInitialized = fn($o) => $ref->isInitialized($o);
                $this->getValue = fn($o) => $ref->getValue($o);
            }
            public function __call($name, $arguments)
            {
                $fn = $this->$name;
                return $fn(...$arguments);
            }
    }
class myclass {
function __call($method, $args) {
if (isset($this->$method)) {
$func = $this->$method;
return call_user_func_array($func, $args);
}
}
}
$obj = new myclass();
 $obj->method = function($var) { echo $var; };
$obj->method('a');

或者您可以创建defolt类并使用。。。