-
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
Intuitive way to convert constructor to closure #12336
Comments
A constructor is essentially just an instance method that is called after constructing a new object. I do agree that conceptually most people would intuitively understand |
The RFC explains what the expectation would be:
That seems pretty straight forward - it's exactly what I was expecting. But then it goes on to say:
I'm not sure why having this feature work consistently for constructor calls is considered a step backwards?
This looks like a consistent and a logical expectation to me. In either case, it's of course not as simple as merely producing a "trampoline function" like the RFC suggests - in either case, what actually needs to happen is you copy (or reference) the parameter list, type-hints and all, so things like reflection work. If I was to write an RFC, that's all it would say: the paragraph from the existing RFC, plus this paragraph about making it work with reflection. But perhaps we should hear from the RFC author before writing another RFC? |
It seems you think that constructors work like factory methods. They don't. Not in the slightest. Really, it's only a small number of major languages that work that way*, while every other language treats them as a special mechanism. No, Realize that constructors are not static methods, that they don't return anything, that If you want closure-ified object construction then that's one thing, but saying "you can do this with methods so it should work with constructors too" is naive.
Can I assume you mean that in jest? Because it would be a very effective way of making sure nobody would support the RFC. Such a short RFC would suggest the author didn't put much time or effort into thinking about what they were proposing. For instance, whether they considered ("for consistency") whether this should be possible on cloning too. * Rust's |
@damianwadley you're explaining how it's implemented - the implementation details are besides the point.
what I would want out of this feature is essentially for
where the parameter list would be identical to the constructor of if the implementation details aren't simple, the concept and the semantics certainly are both simple and intuitive.
I honestly don't know how to stamp it out with any greater verbosity - it's not a complex feature. What else would you like an RFC to say? |
Okay, so I'd like to add two things. First, my motivation for this proposal was for dependency injection - I would have liked to be able to convert a constructor to a closure for the purpose of obtaining a simple factory-function, which I could use in a DI container. However, I started really thinking about how this would work in practice, and I realized, it's not actually going to work. What I wanted for the DI container I've been tinkering with, was to access the argument types of the factory function - and while that would be possible, the performance implications (for most ordinary PHP projects) simply wouldn't be acceptable. The simple realization is that, in order to reflect the constructor arguments, you would of course need to autoload the class - which, in the case of my DI container, which tries to validate the entire container ahead of time, that would, in practice, mean autoloading every type that's been bootstrapped. 😱 In other words, the performance implications of being able to promote a constructor to a closure are too unpredictable, and doesn't really jive with how PHP code generally relies on loading everything as late as possible. I mean, it's not unthinkable that someone might find it useful to be able to convert a constructor to a closure anyway, but it's no use for the thing I had in mind. I'm sorry if I came off short - before thinking through the implementation details, it seemed obvious to me that this would "just work", but I'm guessing unpredictable performance might be one reason why someone chose not to implement this in the first place. You may consider this idea withdrawn, and feel free to close this issue if you don't think this feature is a good idea. |
Thanks @mindplay-dk for your response. |
Description
I wish one of these ways worked:
I'd expect both approaches to work - a constructor is conceptually just a static function that generates a new instance, although the reflection facility doesn't treat it or report it as such.
The second approach at least ought to work - for consistency with that feature.
Even
new Foo(...)
doesn't work - and judging by the error message, someone predicted what I'm trying to do, but it just isn't supported... yet? is this feature planned?(I know there are ways to do it, using reflection, so you don't need to explain that - just wondering why this feature isn't available, as it would seem natural to expect this to work.)
The text was updated successfully, but these errors were encountered: