Content · Programming · PHP

PHP ReflectionProperty::isLazy() Method: Usage and Examples

Checks whether a property on a lazy object is still configured for lazy initialization.

1 views Published Oct 17, 2026 Updated Oct 17, 2026 Know-Stuff

When building tools that inspect PHP code, ReflectionProperty::isLazy() helps answer a focused question: checks whether a property on a lazy object is still configured for lazy initialization.

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

What Is ReflectionProperty::isLazy()?


Checks whether a property on a lazy object is still configured for lazy initialization.

Its method signature is:

public function isLazy(object $object): bool

Parameters:

  • $object — Object containing the property to read or update (not needed for a static property) Required.

Return value: bool

Raw property access and lazy-object controls can bypass normal hooks or initialization. These APIs are intended for controlled framework and persistence tasks.

Example 1: Inspect a property declaration


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

<?php

declare(strict_types=1);

#[Attribute(Attribute::TARGET_PROPERTY)]
class FieldLabel { public function __construct(public string $label) {} }
class BaseData { protected string $source = 'local'; }
class CustomerData extends BaseData {
    #[FieldLabel('identity')]
    public string $name = 'Morgan';
    public private(set) string $token = 'abc';
    public protected(set) int $level = 1;
    public readonly int $identifier;
    public static int $counter = 3;
    public string $label { get => strtoupper($this->name); }
    public string $initials { get => substr($this->name, 0, 1); }
    final public string $finalLabel { get => $this->name; }
    public int $delayed;
    public function __construct(public string $city = 'Rome') { $this->identifier = 10; }
}
$propertyReflection = new ReflectionProperty(CustomerData::class, "name");
$customer = new CustomerData("Oslo");
$classReflection = new ReflectionClass(CustomerData::class);
$customer = $classReflection->newLazyGhost(static function (CustomerData $instance): void { $instance->name = "Loaded"; $instance->city = "Berlin"; $instance->identifier = 10; });

$result = $propertyReflection->isLazy($customer);
var_dump($result);

Output:

bool(true)

Explanation: Checks whether a property on a lazy object is still configured for lazy initialization.

Example 2: Explore another property context


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

<?php

declare(strict_types=1);

#[Attribute(Attribute::TARGET_PROPERTY)]
class FieldLabel { public function __construct(public string $label) {} }
class BaseData { protected string $source = 'local'; }
class CustomerData extends BaseData {
    #[FieldLabel('identity')]
    public string $name = 'Morgan';
    public private(set) string $token = 'abc';
    public protected(set) int $level = 1;
    public readonly int $identifier;
    public static int $counter = 3;
    public string $label { get => strtoupper($this->name); }
    public string $initials { get => substr($this->name, 0, 1); }
    final public string $finalLabel { get => $this->name; }
    public int $delayed;
    public function __construct(public string $city = 'Rome') { $this->identifier = 10; }
}
$propertyReflection = new ReflectionProperty(CustomerData::class, "name");
$customer = new CustomerData("Oslo");
$classReflection = new ReflectionClass(CustomerData::class);
$customer = $classReflection->newLazyGhost(static function (CustomerData $instance): void { $instance->name = "Loaded"; $instance->city = "Berlin"; $instance->identifier = 10; });

$result = $propertyReflection->isLazy($customer);
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: Requires an object argument; ordinary non-lazy objects return false.

Practical Example: Apply reflection to a data model


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

<?php

declare(strict_types=1);

#[Attribute(Attribute::TARGET_PROPERTY)]
class FieldLabel { public function __construct(public string $label) {} }
class BaseData { protected string $source = 'local'; }
class CustomerData extends BaseData {
    #[FieldLabel('identity')]
    public string $name = 'Morgan';
    public private(set) string $token = 'abc';
    public protected(set) int $level = 1;
    public readonly int $identifier;
    public static int $counter = 3;
    public string $label { get => strtoupper($this->name); }
    public string $initials { get => substr($this->name, 0, 1); }
    final public string $finalLabel { get => $this->name; }
    public int $delayed;
    public function __construct(public string $city = 'Rome') { $this->identifier = 10; }
}
$propertyReflection = new ReflectionProperty(CustomerData::class, "name");
$customer = new CustomerData("Oslo");
$classReflection = new ReflectionClass(CustomerData::class);
$customer = $classReflection->newLazyGhost(static function (CustomerData $instance): void { $instance->name = "Loaded"; $instance->city = "Berlin"; $instance->identifier = 10; });

$result = $propertyReflection->isLazy($customer);
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: isInitialized checks actual initialization state.

Important Behavior and Edge Cases


  • Requires an object argument; ordinary non-lazy objects return false.
  • isInitialized checks actual initialization state.
  • Reflection describes declarations and runtime state; requesting metadata does not automatically validate or sanitize application input.

Comparison With Related Approaches


ApproachWhen to Use It
ReflectionProperty::isLazy()Checks whether a property on a lazy object is still configured for lazy initialization.
Related APIisInitialized checks actual initialization state.

Common Beginner Mistakes


MistakeCorrect Approach
Ignoring this condition: Requires an object argument; ordinary non-lazy objects return false.Handle the specific limitation: Requires an object argument; ordinary non-lazy objects return false.
Confusing this method with Related APIisInitialized checks actual initialization state.

Key Takeaways


  • Checks whether a property on a lazy object is still configured for lazy initialization.
  • Requires an object argument; ordinary non-lazy objects return false.
  • isInitialized checks actual initialization state.

Use ReflectionProperty::isLazy() 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.