[DOC-BUGS] Doc #81386 [Com]: ReflectionProperty::getDefaultValue() and promoted properties
[email protected] ("rafr at generaxion dot dk") Fri, 17 May 2024 06:36:06 +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: rafr at generaxion dot dk
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:
Honestly I can't find clear documentation outside of this bug report, which stumped me in fixing a queue issue where someone had updated a class constructor
If we could get a promoted property's default value (assuming it has one within the construct), it would as simple as
$reflection = new \ReflectionClass($this);
$props = $reflection->getProperties();
foreach($props as $prop) {
if (!$prop->isInitialized($this) && $prop->hasDefaultValue()) {
$prop->setValue($this, $prop->getDefaultValue());
}
}
But as is, another loop is needed on top of the previous to ensure all defaults are initialized
foreach($reflection->getConstructor()->getParameters() as $param) {
if (!$param->isPromoted() || !$param->isOptional()) {
continue;
}
$property = $reflection->getProperty($param->getName());
if ($property->isInitialized($this)) {
continue;
}
if ($param->isDefaultValueAvailable()) {
$property->setValue($this, $param->getDefaultValue());
}
}
Previous Comments:
------------------------------------------------------------------------
[2023-06-08 09:35:54] evgeeek at gmail dot com
Hello!
You can get default values from promoted properties in another way:
readonly class A
{
public function __construct(
string $foo,
private int $bar = 5,
) {
}
}
$ro = new ReflectionClass(A::class);
foreach ($ro->getConstructor()->getParameters() as $param) {
var_export($param->name);
var_export($param->isPromoted());
var_export($param->getType()->getName());
var_export($param->isDefaultValueAvailable());
if ($param->isDefaultValueAvailable()) {
var_export($param->getDefaultValue());
}
}
//'foo', false, 'string', false
//'bar', true, 'int', true, 5
------------------------------------------------------------------------
[2023-05-02 18:33:09] kevinhawks14 at aol dot com
(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
------------------------------------------------------------------------
[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.
------------------------------------------------------------------------
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=81386
--
Edit this bug report at https://bugs.php.net/bug.php?id=81386&edit=1