|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2019-08-30 21:27 UTC] manchokapitancho at gmail dot com
Description:
------------
The new PHP 7.4 feature - typed properties is a great addition to the language.
As a side effect it adds a brand new state of the class/object properties - initialized=yes/no.
If one tries to access an uninitialized property, a TypeError is being thrown.
isset($object->prop) does not trigger TypeError but it returns false if $object->prop is NULL.
The suggested check is only indirectly possible via the long construct
(new ReflectionProperty(<name>::class, '<prop>'))->isInitialized($obj)
On the other side, unset works perfectly well and returns a property back into the uninitialized state.
This write-only asymmetry is not consistent and I would expect a language construct similar to isset [e.g. is_initialized($obj->prop)] that handles this case.
Test script:
---------------
class test {
public ?int $prop;
}
$obj = new test;
var_dump(isset($obj->prop));
var_dump(is_initialized($obj->prop));
$obj->prop = null;
var_dump(isset($obj->prop));
var_dump(is_initialized($obj->prop));
Expected result:
----------------
false
false
false
true
Actual result:
--------------
false
<missing>
false
<missing>
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 17 15:00:01 2025 UTC |
A simplified use case would be: //orm with lazy loading from DB class something { public ?<class|self> $parent; .... function getParent() { if (! is_initialized($this->parent)) { $this->parent = //fetch from DB; value could be NULL } return $this->parent; } ... } A plain function is indeed possible but the call parameters should be ($obj, 'prop) since ($obj->prop) would throw TypeError.I know that I can use __get but: 1. this is a workaround (which is also not simpler than a ReflectionProperty call). 2. this is only possible if the developer has control over the class. A counterexample would be a typed property in a class exposed by a third party library. Once again: the target is "if (!is_initialized($obj->prop)) { ... }" similar to "if (!isset($obj->prop)) { ... }". Isset would not generate a notice if the property is not defined and similarly is_initialized should not trigger a TypeError if $obj->prop has not been yet initialized.