Content · Web Development · PHP

PHP ReflectionClass::isUninitializedLazyObject() Method: Usage and Examples

Detects a lazy object that has not yet completed initialization.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 admin

When building tools that inspect PHP code, ReflectionClass::isUninitializedLazyObject() helps answer a focused question: detects a lazy object that has not yet completed initialization.

Reflection is useful in framework tooling, dependency injection, testing, code inspection and debugging.

What Is ReflectionClass::isUninitializedLazyObject()?


Detects a lazy object that has not yet completed initialization.

Its method signature is:

public function isUninitializedLazyObject(object $object): bool

Parameters:

  • $object — Target object that the reflection operation will inspect or modify Required.

Return value: bool

Example 1: Inspect a PHP class declaration


Start with a self-contained PHP declaration and inspect the method result.

<?php

declare(strict_types=1);

#[Attribute(Attribute::TARGET_CLASS_CONSTANT | Attribute::TARGET_METHOD | Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::TARGET_FUNCTION)]
class GuideTag { public function __construct(public string $label) {} }
trait HasIdentifier { public function identifier(): int { return 20; } }
interface NamedEntity { public function name(): string; }
class BaseEntity { public const string BASE = 'entity'; protected int $revision = 1; public static int $total = 4; }
class Shipment extends BaseEntity implements NamedEntity, IteratorAggregate {
    use HasIdentifier;
    #[GuideTag('category')]
    final public const string TYPE = 'parcel';
    private const int SECRET = 5;
    public string $title = 'Box';
    public function __construct(public int $number = 10) {}
    public function name(): string { return $this->title; }
    public function getIterator(): Traversable { yield 'item' => $this->title; }
    public function deliver(string $destination = 'office'): string { return 'Sent to ' . $destination; }
}
readonly class ImmutableShipment { public function __construct(public string $title = 'Box') {} }

$classReflection = new ReflectionClass(Shipment::class);
$initializer = static function (Shipment $shipment): void { $shipment->title = "Loaded 1"; $shipment->number = 30; };
$factory = static function (): Shipment { return new Shipment(40); };
$lazyShipment = $classReflection->newLazyGhost($initializer);

$result = $classReflection->isUninitializedLazyObject($lazyShipment);
var_dump($result);

Output:

bool(true)

Explanation: Detects a lazy object that has not yet completed initialization.

Example 2: Compare a different class target


Next, change the reflected declaration or parameter to observe a related situation.

<?php

declare(strict_types=1);

#[Attribute(Attribute::TARGET_CLASS_CONSTANT | Attribute::TARGET_METHOD | Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::TARGET_FUNCTION)]
class GuideTag { public function __construct(public string $label) {} }
trait HasIdentifier { public function identifier(): int { return 20; } }
interface NamedEntity { public function name(): string; }
class BaseEntity { public const string BASE = 'entity'; protected int $revision = 1; public static int $total = 4; }
class Shipment extends BaseEntity implements NamedEntity, IteratorAggregate {
    use HasIdentifier;
    #[GuideTag('category')]
    final public const string TYPE = 'parcel';
    private const int SECRET = 5;
    public string $title = 'Box';
    public function __construct(public int $number = 10) {}
    public function name(): string { return $this->title; }
    public function getIterator(): Traversable { yield 'item' => $this->title; }
    public function deliver(string $destination = 'office'): string { return 'Sent to ' . $destination; }
}
readonly class ImmutableShipment { public function __construct(public string $title = 'Box') {} }

$classReflection = new ReflectionClass(Shipment::class);
$initializer = static function (Shipment $shipment): void { $shipment->title = "Loaded 2"; $shipment->number = 31; };
$factory = static function (): Shipment { return new Shipment(40); };
$lazyShipment = $classReflection->newLazyGhost($initializer);

$result = $classReflection->isUninitializedLazyObject($lazyShipment);
var_dump($result);

// Check the returned value type before using the result.
echo "\nResult type: " . get_debug_type($result);

Output:

bool(true)

Result type: bool

Explanation: An initialized lazy object and an ordinary instance both return false.

Practical Example: Apply reflection to a real object model


This final case shows how the same API can be used when inspecting application definitions.

<?php

declare(strict_types=1);

#[Attribute(Attribute::TARGET_CLASS_CONSTANT | Attribute::TARGET_METHOD | Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::TARGET_FUNCTION)]
class GuideTag { public function __construct(public string $label) {} }
trait HasIdentifier { public function identifier(): int { return 20; } }
interface NamedEntity { public function name(): string; }
class BaseEntity { public const string BASE = 'entity'; protected int $revision = 1; public static int $total = 4; }
class Shipment extends BaseEntity implements NamedEntity, IteratorAggregate {
    use HasIdentifier;
    #[GuideTag('category')]
    final public const string TYPE = 'parcel';
    private const int SECRET = 5;
    public string $title = 'Box';
    public function __construct(public int $number = 10) {}
    public function name(): string { return $this->title; }
    public function getIterator(): Traversable { yield 'item' => $this->title; }
    public function deliver(string $destination = 'office'): string { return 'Sent to ' . $destination; }
}
readonly class ImmutableShipment { public function __construct(public string $title = 'Box') {} }

$classReflection = new ReflectionClass(Shipment::class);
$initializer = static function (Shipment $shipment): void { $shipment->title = "Loaded 3"; $shipment->number = 32; };
$factory = static function (): Shipment { return new Shipment(40); };
$lazyShipment = $classReflection->newLazyGhost($initializer);

$result = $classReflection->isUninitializedLazyObject($lazyShipment);
var_dump($result);

// Use the reflection result in a simple decision.
if (is_bool($result)) {
    echo "\nStatus: " . ($result ? "yes" : "no");
} elseif (is_array($result)) {
    echo "\nEntries: " . count($result);
} elseif ($result === null) {
    echo "\nNo value returned";
} else {
    echo "\nReflection result available";
}

Output:

bool(true)

Status: yes

Explanation: getLazyInitializer returns its initializer or factory, not a boolean flag.

Important Behavior and Edge Cases


  • An initialized lazy object and an ordinary instance both return false.
  • getLazyInitializer returns its initializer or factory, not a boolean flag.
  • Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.

Comparison With Related Approaches


ApproachWhen to Use It
ReflectionClass::isUninitializedLazyObject()Detects a lazy object that has not yet completed initialization.
Related APIgetLazyInitializer returns its initializer or factory, not a boolean flag.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring this condition: An initialized lazy object and an ordinary instance both return false.Handle the specific limitation: An initialized lazy object and an ordinary instance both return false.
Confusing this method with Related APIgetLazyInitializer returns its initializer or factory, not a boolean flag.

Key Takeaways


  • Detects a lazy object that has not yet completed initialization.
  • An initialized lazy object and an ordinary instance both return false.
  • getLazyInitializer returns its initializer or factory, not a boolean flag.

Use ReflectionClass::isUninitializedLazyObject() when you need the precise metadata or behavior demonstrated above. Keep the target declaration and relevant PHP version in mind when adapting the examples.

Share this resourceSend this page to someone who may find it useful.