gpt4 book ai didi

php - 实例化一个对象而不在 PHP 中调用它的构造函数

转载 作者:IT王子 更新时间:2023-10-29 00:07:40 25 4
gpt4 key购买 nike

为了恢复已持久化的对象的状态,我想创建一个类的空实例,而不调用其构造函数,以便稍后使用 Reflection 设置属性.

我找到的唯一方法,就是这条路Doctrine确实,是创建对象的伪序列化,并对其进行unserialize():

function prototype($class)
{
$serialized = sprintf('O:%u:"%s":0:{}', strlen($class), $class);
return unserialize($serialized);
}

有没有另一种hacky的方式来做到这一点?

我期待在 Reflection 中找到这样的方法,但我没有。

最佳答案

更新: ReflectionClass::newInstanceWithoutConstructor从 PHP 5.4 开始可用!

关于php - 实例化一个对象而不在 PHP 中调用它的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6944946/

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