Skip to content

newLazyProxy($closure) throws error on $closure result null, but can't capture it. #19224

@procodix

Description

@procodix

Description

The following code:

try {
	class Test {
		public string $Foo = 'Bar';
	}
	$proxy = new ReflectionClass(Test::class)->newLazyProxy(fn() => null);
	$proxy->Foo;
} catch (Throwable $x) {
	// do something...
}

Resulted in this output:

Error: Lazy proxy factory must return an instance of a class compatible with Zok\System\Test, null returned

But I expected instead:

  • allow "null" to be returned from the closure, so that its signature be like: function($ghost):?object
  • allow the error thrown to be captured and instead return null, and replace the proxy shell with null

BTW this would could collapse ghosts and proxies to a single lazy concept, allowing to return

  • a previously empty shell object, now filled with
  • a freshly created object
  • null. This is important if -for example- a database lookup returned no result

### PHP Version

```plain
PHP 8.4

Operating System

macOS

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions