Bug #78480 [Com]: The typed properties miss an important "function"

From: Date: Sun, 08 Sep 2019 18:10:54 +0000
Subject: Bug #78480 [Com]: The typed properties miss an important "function"
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-222629@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78480&edit=1 ID: 78480 Comment by: jhdxr@php.net Reported by: manchokapitancho at gmail dot com Summary: The typed properties miss an important "function" Status: Open Type: Bug Package: Scripting Engine problem PHP Version: 7.4.0beta4 Block user comment: N Private report: N New Comment: You can use __get to check if it's initialized. see the Overloaded Properties section of the RFC(https://wiki.php.net/rfc/typed_properties_v2) as an example. Previous Comments: ------------------------------------------------------------------------ [2019-08-31 14:05:20] manchokapitancho at gmail dot com 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. ------------------------------------------------------------------------ [2019-08-31 11:42:08] nikic@php.net What's your use-case? A language construct for this is very unlikely, but if this is a common and performance-critical operation, a plain function could be provided in lieu of going through reflection. ------------------------------------------------------------------------ [2019-08-30 21:27:50] 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> ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=78480&edit=1

« previous php.bugs (#222629) next »