php8.5
Home/ Manual/ reflection / reflectionproperty/ ReflectionProperty::isInitialized

ReflectionProperty::isInitialized

PHP function Edit on GitHub ✎

(PHP 7 >= 7.4.0, PHP 8)

Checks whether a property is initialized

Description

ReflectionProperty::isInitialized(object|null $object = null): bool

Checks whether a property is initialized.

Parameters

object

If the property is non-static an object must be provided to fetch the property from.

Return Values

Returns false for typed properties prior to initialization, and for properties that have been explicitly unset(). For all other properties true will be returned.

Errors/Exceptions

Throws a ReflectionException if the property is inaccessible. You can make a protected or private property accessible using ReflectionProperty::setAccessible.

Changelog

VersionDescription
8.0.0object is nullable now.

Examples

ReflectionProperty::isInitialized example

php
<?php
class User
{
    public string $name;
}

$rp = new ReflectionProperty('User', 'name');
$user = new User;
var_dump($rp->isInitialized($user));
$user->name = 'Nikita';
var_dump($rp->isInitialized($user));
?>

The above example will output:

output
bool(false)
bool(true)

See Also

  • ReflectionProperty::hasType

Source: reference/reflection/reflectionproperty/isinitialized.xml · from the official PHP manual (php/doc-en)