-
Notifications
You must be signed in to change notification settings - Fork 7.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
object-bound closures for initalization, modification of objects #17936
Comments
You're looking for something like this proposal, which was declined. https://wiki.php.net/rfc/compact-object-property-assignment |
Not really. The proposal you mention is just for assignments. This has no added value over default values in constructors. Im not suggesting some limited functionality inside the block. In my example one could write all PHP code to init or modify the object. More like a configuring closure. Difference to current possibilities is the sleeker syntax and rebinding of the "this" variable to save the declaration of a temporary variable. |
I'm not sure why we need to reinvent the wheel. https://3v4l.org/kXGtb Do you know of any other language that offers a similar feature? |
Hm, I feel the syntax should be easier. Also, IDEs will have problems, detecting what $this refers to, for example: $this->subelement->modify(fn() => $this->whatever()); Swift uses implicit constructors, where you have full access to all language features. In PHP, promoted constructors only allow constant statements and new (method arguments allow even less as default). Nim is very capable of allowing individual DSLs, but their brevity is due to blocks-by-indentation. |
Most IDEs support generic templates now, although granted they probably won't support https://phpstan.org/r/8a93650f-c7b9-4b19-b7bd-615f3a588c98 I'm not sure making the closure referring to
Method default arguments allow less than property default values? I don't think that's true. E.g. |
So, this feature request is very vague, and out of scope for GH issues anyway. As mentioned in other issues, please raise a discussion on the internals mailing list. Feature requests on GH that require RFCs (as this one) auto-close after 3 months, so if you want to push this forward, a discussion with the wider community will achieve more. |
Description
Objects (and nested ones) are often constructed with local variables:
This can also be achieved by constructor parameters:
Can this be done more elegantly?
Problem arises, when we have objects with too many properties. Of course we can use leverage properties and named arguments, but the constructors declarations become lengthy. Even more since we can also define property hooks within the promoted properties. But things get even more complicated, with inheritance and constructing objects with a mixture of ordered params, named params and even variadic params to be passed - for example - into the parent constructor.
Example is the creation of HTML DSL:
This gives me some problems.
Closures can solve this, but they are bulky, especially the non-shorthand version:
Can we have a smoother syntax for this:
Maybe an operator would make sense to execute a block on an object's context:
The text was updated successfully, but these errors were encountered: