Doc #81386 [Com]: ReflectionProperty::getDefaultValue() and promoted properties
[email protected] ("kevinhawks14 at aol dot com") Tue, 02 May 2023 18:33:09 +0000
| Newsgroups | php.doc.bugs |
|---|---|
| Message-ID | <[email protected]> |
Edit report at https://bugs.php.net/bug.php?id=81386&edit=1
ID: 81386
Comment by: kevinhawks14 at aol dot com
Reported by: enumag at gmail dot com
Summary: ReflectionProperty::getDefaultValue() and promoted
properties
Status: Open
Type: Documentation Problem
Package: Documentation problem
PHP Version: 8.0.9
Block user comment: N
Private report: N
New Comment:
(https://collegegrazing.com/)github.com
(https://collegegrazing.com/blog/)github.com
(https://collegegrazing.com/affiliate-disclosure/)github.com
(https://collegegrazing.com/terms-of-use/)github.com
(https://collegegrazing.com/fake-id/best-fake-id-websites/)github.com
(https://collegegrazing.com/about/)github.com
(https://collegegrazing.com/contact/)github.com
(https://collegegrazing.com/fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-fake-ids/)github.com
(https://collegegrazing.com/how-to-get-a-fake-id/)github.com
(https://collegegrazing.com/top-fake-id-review/)github.com
(https://collegegrazing.com/fake-id/buy-virginia-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-new-mexico-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-kentucky-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-iowa-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-maine-fake-id/)github.com
(https://collegegrazing.com/all-state-fakes-review/)github.com
(https://collegegrazing.com/fake-id/buy-colorado-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-missouri-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-new-hampshire-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-vermont-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-georgia-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-arizona-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-alabama-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-florida-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-maryland-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-wyoming-fake-id/)github.com
(https://collegegrazing.com/fake-id/what-happens-if-i-get-caught/)github.com
(https://collegegrazing.com/fake-id/buy-california-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-new-york-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-alaska-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-rhode-island-fake-id/)github.com
(https://collegegrazing.com/magic-fake-id-review/)github.com
(https://collegegrazing.com/fake-id/buy-utah-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-hawaii-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-minnesota-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-oklahoma-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-illinois-fake-id/)github.com
(https://collegegrazing.com/evolved-ids-review/)github.com
(https://collegegrazing.com/fake-id/buy-indiana-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-montana-fake-id/)github.com
(https://collegegrazing.com/how-do-bouncers-spot-fake-ids/)github.com
(https://collegegrazing.com/fake-id/common-uses/)github.com
(https://collegegrazing.com/fake-id/buy-arkansas-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-pennsylvania-fake-id/)github.com
(https://collegegrazing.com/blog/page/2/)github.com
(https://collegegrazing.com/fake-id/buy-new-jersey-fake-id/)github.com
(https://collegegrazing.com/how-to-spot-a-georgia-fake-id/)github.com
(https://collegegrazing.com/scannable-fake-ids/)github.com
(https://collegegrazing.com/fake-id/buy-south-dakota-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-massachusetts-fake-id/)github.com)github.com
(https://collegegrazing.com/fake-id/how-to-spot/)github.com
(https://collegegrazing.com/fake-id/buy-west-virginia-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-connecticut-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-wisconsin-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-idaho-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-columbia-fake-id/)github.com
(https://collegegrazing.com/fake-id/how-to-make/)github.com
(https://collegegrazing.com/fake-id/buy-mississippi-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-nevada-fake-id/)github.com
(https://collegegrazing.com/how-to-pay-for-fake-id/)github.com
(https://collegegrazing.com/how-to-take-a-fake-id-photo-at-home/)github.com
(https://collegegrazing.com/fake-id/buy-oregon-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-texas-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-north-dakota-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-louisiana-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-south-carolina-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-kansas-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-tennessee-fake-id/)github.com
(https://collegegrazing.com/fake-id/types-of-fake-ids/)github.com
(https://collegegrazing.com/fake-id/buy-delaware-fake-id/)github.com
(https://collegegrazing.com/lost-identification-review/)github.com
(https://collegegrazing.com/fake-id/buy-washington-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-nebraska-fake-id/)github.com
(https://collegegrazing.com/dingo-fakes-review/)github.com
(https://collegegrazing.com/fake-id/buy-north-carolina-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-michigan-fake-id/)github.com
(https://collegegrazing.com/fake-id/buy-ohio-fake-id/)github.com
(https://collegegrazing.com/fake-id/best-states/)github.com
(https://collegegrazing.com/idviking-review/)github.com
(https://collegegrazing.com/super-printer-bros-review/)github.com
(https://collegegrazing.com/idgod-review/)github.com
(https://collegegrazing.com/bogus-braxtor-review/)github.com
(https://collegegrazing.com/newids-review/)github.com
(https://collegegrazing.com/litfakes-review/)github.com
(https://collegegrazing.com/old-ironside-fakes-review/)github.com
(https://collegegrazing.com/idhurry-review/)github.com
Previous Comments:
------------------------------------------------------------------------
[2022-12-29 15:02:25] hugo dot nicolas dot hn at gmail dot com
Hello, I also stumbled across this "issue" today, and I am in no place to say if this is supposed to be a bug or not, but I wanted to add my experience, being that in addition, if a property is promoted but not typed, its associated ReflectionProperty::hasDefaultValue() seems to always return true.
Test script:
-------------
<?php
class Foo {
public function __construct(
public $foo,
public $bar = 'bar'
) {
}
}
$reflection = new ReflectionClass(Foo::class);
$fooReflection = $reflection->getProperty('foo');
$barReflection = $reflection->getProperty('bar');
var_dump($fooReflection->hasDefaultValue()); // true
var_dump($fooReflection->getDefaultValue()); // null
var_dump($barReflection->hasDefaultValue()); // true
var_dump($barReflection->getDefaultValue()); // null
------------------------------------------------------------------------
[2021-08-26 10:45:54] enumag at gmail dot com
@nikic I have to disagree. As a programmer who is using promoted properties I don't differentiate if the default value is for the property or the parameter. Even if the behavior is expected from the language's point of view, it's not entirely expected from the user's point of view. I'd much rather see it mentioned there than only randomly stumbling on it while debugging.
------------------------------------------------------------------------
[2021-08-26 10:35:40] [email protected]
Promoted properties intentionally don't have a default value. The property is always initialized by the constructor.
I don't think this needs to be mentioned in the ReflectionProperty::getDefaultValue() docs, as reflection simply reflects reality. However, it should be mentioned in the promoted property docs at https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion.
------------------------------------------------------------------------
[2021-08-26 10:33:43] enumag at gmail dot com
Forgot to mention: ReflectionProperty::hasDefaultValue() needs to be fixed or better documented as well.
------------------------------------------------------------------------
[2021-08-26 10:30:38] enumag at gmail dot com
Description:
------------
Currently when you declare a promoted property with a default value the ReflectionProperty::getDefaultValue() won't return that value.
I'm not sure if this behavior is expected or not but in either case something needs to be changed.
If this behavior is unexpected then it should be fixed to return the correct value.
If this behavior is expected then the documentation of ReflectionProperty::getDefaultValue() should mention this drawback along with a few lines of code how to get to the relevant ReflectionParameter and get the value there.
Test script:
---------------
<?php
class X {
public function __construct(
public bool $y = false
) {
}
}
var_export((new ReflectionProperty('X', 'y'))->getDefaultValue());
Expected result:
----------------
Either the value should be returned or this behavior needs to be documented.
Actual result:
--------------
The default value is not returned and there is no mention of this behavior in the documentation
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=81386&edit=1