Bug #78904 [Csd]: Uninitialized property triggers __get()
| From: | public at grik dot net | Date: | Mon, 20 Jan 2020 19:13:15 +0000 |
| Subject: | Bug #78904 [Csd]: Uninitialized property triggers __get() | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-225014@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=78904&edit=1
ID: 78904
User updated by: public at grik dot net
Reported by: public at grik dot net
Summary: Uninitialized property triggers __get()
Status: Closed
Type: Bug
Package: *General Issues
PHP Version: 7.4.0
Assigned To: nikic
Block user comment: N
Private report: N
New Comment:
Andrzej, dependency injection based on property types is contract programming, while
annotation-based injection (you name it lazy loading) is a declarative programming.
These approaches should not be compared cause they follow ideologically different approach to system
design. A sample of approaches is Yii vs Symfony.
I don't know they purpose of the code, but in general case I would advice against utilizing
undocumented side effects in core code used in production, such as service locator and dependency
injection. Low-level hacks are OK for debugging, mocking and testing utilites.
Previous Comments:
------------------------------------------------------------------------
[2020-01-20 13:19:21] andrzej dot heczko at gmail dot com
Thank you for reply.
By doing unset first I have to explicitly put all the unset thing in constructor. In my opinion,
this comes with time and resource penalty and makes code less readable.
Before fix (7.4.0):
class A {
use GetMagicTrait;
private Foo $a;
private Bar $b;
private Baz $z;
}
After fix (7.4.1+):
class A {
use GetMagicTrait;
private Foo $a;
private Bar $b;
private Baz $z;
function __construct()
{
unset($this->$a, $this->b, $this->z);
}
}
Anyway I have read the rfc for typed properties briefly and if I understand it correctly, this fix
breaks it starting with section https://wiki.php.net/rfc/typed_properties_v2#uninitialized_and_unset_properties.
I would like to explain, what I want to achieve.
As of php7.4 not nullable typed properties can have 3 different states:
1. Not Initialized
2. Not Set (Initialized)
3. Set (Initialized)
So whenever I use unset I'm making its state unset (2) and __get is working.
Problem is, we cannot define typed property as unset, only "Not Initialized" or
"Initialized"
I would like to be able to define unset state at the property instead of "Not
Initialized". This would avoid using explicit unset in constructors.
I like typescript ! operator so final property definition could use one of:
class A {
use GetMagicTrait;
private Foo $a!;
private Bar! $b;
private ! Baz $z;
private unset Bay $f;
}
where "private unset Foo $a" could be equivalent to shorter use with !.
Can I write RFC for this?
Are there any guidelines for RFCs?
What timeline could be expected for implementation?
------------------------------------------------------------------------
[2020-01-17 10:04:24] nikic@php.net
@andrzej: Lazy property initialization is still supported and used by ProxyManager for example. To
lazily initialize both typed and untyped properties, you must unset() them in the constructor.
------------------------------------------------------------------------
[2020-01-17 09:57:06] andrzej dot heczko at gmail dot com
I was experimenting with this behavior to allow lazy property initialization. For instance I can
lazy load services in controllers. Now there is no other option, how to allow lazy initialize
property. I have to stick back to annotations and dynamic properties.
You could add another magic method to allow that e.g. "__init",
"__initProperty", "__getStrict", "__getTyped", etc. which would
respect access modifiers.
You can see all my efforts in composer package bigbit/oddin.
Version 1.* is using annotations and dynamic properties, version 2.* is using typed properties and
__get.
------------------------------------------------------------------------
[2019-12-09 07:43:01] nikic@php.net
Automatic comment on behalf of nikita.ppv@gmail.com
Revision: http://git.php.net/?p=php-src.git;a=commit;h=84354c62b37a56816a695b18ebd898f9703a9ad2
Log: Fixed bug #78904: Don't call any magic for uninitialized typed properties
------------------------------------------------------------------------
[2019-12-06 15:32:10] public at grik dot net
Thank you. Sorry for filling this ticket up improperly, I did not expect attention.
------------------------------------------------------------------------
The remainder of the comments for this report are too long. To view
the rest of the comments, please view the bug report online at
https://bugs.php.net/bug.php?id=78904
--
Edit this bug report at https://bugs.php.net/bug.php?id=78904&edit=1