Skip to content
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

Constructor with explicit argument #60

Closed
giann opened this issue Mar 16, 2022 · 1 comment
Closed

Constructor with explicit argument #60

giann opened this issue Mar 16, 2022 · 1 comment
Labels
wontfix This will not be worked on

Comments

@giann
Copy link

giann commented Mar 16, 2022

I'd like to be able to detect missing/badly typed properties with static analysis.
So the __construct(array $rawModelDataInput = []) is not ideal in that regards since dev will see their error at runtime.

Would it be possible, maybe via a config option, to generate constructor of the form __construct(string $propA, int $propB)?

With php 8.1 we would also be able to use named arguments to avoid unreadable constructors.

@wol-soft
Copy link
Owner

wol-soft commented Mar 16, 2022

Explicit arguments aren't intended by design. They would require the developer, especially for nested objects, to set up the whole object structure manually and pass initialized nested objects to outer objects (eg. if you have a customer and the customer has a address object you'd need to initialize the address object first).

Additionally to the manual initialization of each nested object this would disable collecting all errors from the provided data (eg. invalid address data would throw an error before invalid customer data is detected).

The intention of the objects is, to handle data from a source where you don't pass single variables to the object (eg. you process data via an API).

What you could do if you really want to have the functionality, is to implement a post processor which adds a static factory method which wraps the constructor and provides the arguments.

@wol-soft wol-soft added the wontfix This will not be worked on label Mar 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

2 participants