大网数据广告活动
PHP 语言与运行手册

ReflectionClass::newLazyProxy · Creates a new lazy proxy instance

· 52CE 我爱测

ReflectionClass::newLazyProxy — Creates a new lazy proxy instance

ReflectionClass::newLazyProxy

(PHP 8 >= 8.4.0)

ReflectionClass::newLazyProxy — Creates a new lazy proxy instance

说明

public function ReflectionClass::newLazyProxy(callable $factory, int $options = 0): object

Creates a new lazy proxy instance of the class, attaching the factory function to it. The constructor is not called, and properties are not set to their default values. When an attempt is made to observe or modify the proxy's state for the first time, the factory function is called to provide a real instance, which is then attached to the proxy. After this, all subsequent interactions with the proxy are forwarded to the real instance. See Initialization Triggers and Initialization Sequence.

参数

factory

The factory is a callback with the following signature:

function factory(object $object): object
object

The object being initialized. At this point, the object is no longer marked as lazy, and accessing it does not trigger initialization again.

The factory function must return an object, referred to as the real instance, which is then attached to the proxy. This real instance must not be lazy and must not be the proxy itself. If the real instance does not have the same class as the proxy, the proxy's class must be a subclass of the real instance's class, without additional properties, and must not override the __destruct() or __clone() methods.

options

options can be a combination of the following flags:

ReflectionClass::SKIP_INITIALIZATION_ON_SERIALIZE

By default, serializing a lazy object triggers its initialization. Setting this flag prevents initialization, allowing lazy objects to be serialized without being initialized.

返回值

Returns a lazy proxy instance. If the object has no properties, or if all its properties are static or virtual, a normal (non-lazy) instance is returned. See also Lifecycle of Lazy Objects.

错误/异常

An Error if the class is internal or extends an internal class except stdClass.

示例

示例 #1 Basic usage

<?php
class Example {
    public function __construct(public int $prop) {
        echo __METHOD__, "\n";
    }
}

$reflector = new ReflectionClass(Example::class);
$object = $reflector->newLazyProxy(function (Example $object) {
     $realInstance = new Example(1);
     return $realInstance;
});

var_dump($object);
var_dump($object instanceof Example);

// Triggers initialization, and forwards the property fetch to the real instance
var_dump($object->prop);

var_dump($object);
?>

以上示例会输出:

lazy proxy object(Example)#3 (0) {
  ["prop"]=>
  uninitialized(int)
}
bool(true)
Example::__construct
int(1)
lazy proxy object(Example)#3 (1) {
  ["instance"]=>
  object(Example)#4 (1) {
    ["prop"]=>
    int(1)
  }
}

参见

来源:PHP Documentation Group · 作者与贡献记录 · CC BY 3.0。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
PHP Documentation Group
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。