Skip to content

Property Hooks - reflectionProperty::getInitialized #15694

Closed
@bkdotcom

Description

@bkdotcom

Description

The following code:

https://3v4l.org/KXI3l/rfc#vgit.master

<?php

class PropertyHooks
{
    public ?string $backedGetOnly {
        get => $this->backedGetOnly;
    }

    public ?string $backedSetOnly {
        set (?string $value) {
            $this->backedSetOnly = $value;
        }
    }

    public ?string $backedGetAndSet {
        set (?string $value) {
            $this->backedGetAndSet = $value;
        }

        get => $this->backedGetAndSet;
    }
}

$propertyHooks = new PropertyHooks();

$reflectionProperty = new ReflectionProperty($propertyHooks, 'backedGetOnly');

$reflectionProperty->isInitialized($propertyHooks); // returns true - I would expect false
                                      // backSetOnly reports  false
                                      // backedGetAndSet reports true - I would also expect false

if ($reflectionProperty->isInitialized($propertyHooks)) {
    $reflectionProperty->getRawValue($propertyHooks); // Uncaught exception 'Error' with message Typed property PropertyHooks::$backedGetOnly must not be accessed before initialization
} else {
    echo 'not initialized';
}

Resulted in this output:

Fatal error: Uncaught Error: Typed property PropertyHooks::$backedGetOnly must not be accessed before initialization in /in/KXI3l:33
Stack trace:
#0 [internal function]: PropertyHooks->$backedGetOnly::get()
#1 /in/KXI3l(33): ReflectionProperty->getRawValue(Object(PropertyHooks))

But I expected this output instead:

not initialized

ReflectionProperty::isInitialized() seems to be based on whether or not there's a get hook instead of the backing-value?
If that is expected behavior, should we also have a isRawValueInitialized() method?

(isInitialized() returns true for virtual properties)

PHP Version

PHP 8.4 (master - 2024-09-01)

Operating System

OSX & 3v4l

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions