Bug #78226 [Com]: Unexpected __set behavior

From: Date: Fri, 28 Jun 2019 15:23:07 +0000
Subject: Bug #78226 [Com]: Unexpected __set behavior
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-221553@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=78226&edit=1 ID: 78226 Comment by: dirk dot gerigk at atraveo dot com Reported by: dirk dot gerigk at tui dot com Summary: Unexpected __set behavior Status: Open Type: Bug Package: Class/Object related Operating System: Linux PHP Version: 7.4Git-2019-06-28 (Git) Block user comment: N Private report: N New Comment: Ignore my last question in the last comment. :-) Previous Comments: ------------------------------------------------------------------------ [2019-06-28 15:13:11] dirk dot gerigk at atraveo dot com Thanx for the fast answer. I have taken the time to read the https://wiki.php.net/rfc/typed_properties_v2 in detail and was suprised how complex the hole thing will be. I think will dig into it fully (writing code), when it is implement full in the future. But i am still curious how it will work in the future, so is there a link to a final draft of the implementation? ------------------------------------------------------------------------ [2019-06-28 11:02:54] nikic@php.net This is working "as intended", though I agree that the behavior is quite surprising. Typed properties start out uninitialized/unset and accessing unset properties causes calls to __get/__set. If you can, don't use __set. If you can't, use public ?string $bar = null; to explicitly initialize it the property to null, though you will have to deal with the consequences of that. It would be nice to have a way to bypass __set when assigning to an unset property (including uninitialized typed properties), but I don't see any obvious way to support that. ------------------------------------------------------------------------ [2019-06-28 10:43:53] dirk dot gerigk at tui dot com OK, forget the side note. i have read this https://wiki.php.net/rfc/typed_properties_v2 and it is explained there. ------------------------------------------------------------------------ [2019-06-28 10:26:02] dirk dot gerigk at tui dot com Description: ------------ Maybe it is a not implemented yet in alpha1, but i found a unexpected behavior when using __set / __get. I will only point to one issue i found. So, when you use __set in a class and typ-hint a property, the __set method is always called. Side Note (other issue): Is it normal that you have to make every type-hinted property nullable? Because when you have: public string $bar; And do: (new A)->bar; You get: Fatal error: Uncaught TypeError: Typed property A::$bar must be string, null used Is that wanted in that way? Test script: --------------- class A { public string $bar; public $foo; final public function __get($name){ print "get_$name "; } final public function __set($name,$arg){ print "set_$name "; } } $obj = new A; $obj->bar=''; $obj->foo=''; var_export($obj); Expected result: ---------------- A::__set_state(array( 'bar' => '', 'foo' => '', )) Actual result: -------------- set_bar A::__set_state(array( 'foo' => '', )) ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=78226&edit=1

« previous php.bugs (#221553) next »