Uh oh!
There was an error while loading. Please reload this page.
Implement "Constructor Promotion" - #5291
Conversation
2e86157 to
4200794CompareThere was a problem hiding this comment.
Could add a test of global functions/closures - I'd guess that the following snippet probably has a misleading error message (not sure what scope->properties_info is)
<?phpfunction__construct(public$x) {}LGTM otherwise
There was a problem hiding this comment.
Added a test in 717134e, error message seems fine. Or did you expect something different there?
There was a problem hiding this comment.
Ah sorry, I missed that you had function __construct() in particular in mind here. Changed the name in 7139cdc.
Still works fine though, because is_ctor above includes a check that scope is not null.
There was a problem hiding this comment.
Oh, didn't see the scope part of the check
enumag
commented
Mar 26, 2020
Somehow I'm more interested in the |
okdewit
commented
Mar 26, 2020
@nikic Is there a reason to not go a step further, and use a "Kotlin style" syntax? class Point (
public float $x = 0.0,
public float $y = 0.0,
public float $z = 0.0,
) {
//
} |
7139cdc to
f3dbd18CompareTysonAndre
commented
May 30, 2020
After this change, static arrow functions with parameters don't get the doc comments parsed properly in Reflection. It should be easy to fix by changing An example of affected code: |
nikic
commented
Jun 5, 2020
@TysonAndre Nice catch! It should be fixed now. |
| public function __construct( | ||
| public float $x = 0.0, | ||
| public float $y = 1.0, | ||
| public float $z = 2.0 |
There was a problem hiding this comment.
In the RFC code example the trailing comma is allowed, but not here.
Which is correct?
There was a problem hiding this comment.
Yes, here is PHP Wiki: https://wiki.php.net/rfc/trailing_comma_in_parameter_list
FlorianSteenbuck
commented
Jan 5, 2021
Is there any reflection possibility for visibility ? |
nikic
commented
Jan 5, 2021
@FlorianSteenbuck The visibility should be accessible through ReflectionProperty as usual. |
@nikic ok: https://3v4l.org/JsbXT <?phpclass Point {
publicfunction__construct(
publicfloat$x,
publicfloat$y = 0.0,
publicfloat$z = 0.0,
) {}
}
foreach ((newReflectionClass(Point::class))->getProperties() as$property) {
var_dump($property->name);
} |
zlianon
commented
May 5, 2021
@nikic what is the reason for which it is not allowed to declare promoted property in an abstract constructor? interface InstructionInterface
{
/** * InstructionInterface constructor. * * @param mixed $operand */publicfunction__construct(
privatemixed$operand,
);
/** * @param SplStack $stack */publicfunction__invoke(SplStack$stack): void;
} |
nikic
commented
May 5, 2021
Promoted properties combine a property declaration with initialization of that property in the constructor. There is no way to initialize the property in an abstract constructor. Your example is doubly illegal because it would also require declaring a property in an interface, which is not allowed. |
* RemoveUnusedVariableInCatchRector (https://wiki.php.net/rfc/non-capturing_catches) * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotionphp/php-src#5291) * StrContainsRector (https://externals.io/message/108562php/php-src#5179)
Applied rules: * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotionphp/php-src#5291) * ReadOnlyPropertyRector (https://wiki.php.net/rfc/readonly_properties_v2)
Applied rules: * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotionphp/php-src#5291) * ReadOnlyPropertyRector (https://wiki.php.net/rfc/readonly_properties_v2)
Applied rules: * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotionphp/php-src#5291) * MixedTypeRector * ReadOnlyPropertyRector (https://wiki.php.net/rfc/readonly_properties_v2)
Applied rules: * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotionphp/php-src#5291) * StrStartsWithRector (https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions) * StrContainsRector (https://externals.io/message/108562php/php-src#5179) * ReadOnlyPropertyRector (https://wiki.php.net/rfc/readonly_properties_v2)
Applied rules: * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotionphp/php-src#5291) * StrStartsWithRector (https://wiki.php.net/rfc/add_str_starts_with_and_ends_with_functions) * NullToStrictStringFuncCallArgRector * ReadOnlyPropertyRector (https://wiki.php.net/rfc/readonly_properties_v2)
…ion` sniff PHP 8.0 introduced constructor property promotion. Refs: * https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion * https://www.php.net/manual/en/migration80.new-features.php#migration80.new-features.core.property-promotion * https://wiki.php.net/rfc/constructor_promotion * php/php-src#5291 * php/php-src@064b464 This commit adds a new sniff to detect this. Includes unit tests.
…ion` sniff PHP 8.0 introduced constructor property promotion. Refs: * https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion * https://www.php.net/manual/en/migration80.new-features.php#migration80.new-features.core.property-promotion * https://wiki.php.net/rfc/constructor_promotion * php/php-src#5291 * php/php-src@064b464 This commit adds a new sniff to detect this. Includes unit tests.
…ion` sniff PHP 8.0 introduced constructor property promotion. Refs: * https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion * https://www.php.net/manual/en/migration80.new-features.php#migration80.new-features.core.property-promotion * https://wiki.php.net/rfc/constructor_promotion * php/php-src#5291 * php/php-src@064b464 This commit adds a new sniff to detect this. Includes unit tests.
…ion` sniff PHP 8.0 introduced constructor property promotion. Refs: * https://www.php.net/manual/en/language.oop5.decon.php#language.oop5.decon.constructor.promotion * https://www.php.net/manual/en/migration80.new-features.php#migration80.new-features.core.property-promotion * https://wiki.php.net/rfc/constructor_promotion * php/php-src#5291 * php/php-src@064b464 This commit adds a new sniff to detect this. Includes unit tests.
Applied rules: * LongArrayToShortArrayRector * TernaryToNullCoalescingRector * ClassPropertyAssignToConstructorPromotionRector (https://wiki.php.net/rfc/constructor_promotionphp/php-src#5291) * MixedTypeRector * ChangeSwitchToMatchRector (https://wiki.php.net/rfc/match_expression_v2) * NullToStrictStringFuncCallArgRector * TypedPropertyFromAssignsRector
RFC: https://wiki.php.net/rfc/constructor_promotion
As recently discussed on list, this implements the following short hand syntax:
This desugars to: