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

Optimizing constructor and argument initialization #91

Open
phphleb opened this issue Mar 17, 2024 · 3 comments
Open

Optimizing constructor and argument initialization #91

phphleb opened this issue Mar 17, 2024 · 3 comments

Comments

@phphleb
Copy link

phphleb commented Mar 17, 2024

I propose to add an alternative syntax for a simplified notation of the constructor and its arguments.
For example:

use \Yiisoft\Definitions\ArrayDefinition;

$definition = ArrayDefinition::fromConfig([
    'class' => MyServiceInterface::class,
    '__construct()' => [42], 
    '$propertyName' => 'value',
    'setName()' => ['Alex'],
]);

So it can be written like this:

use \Yiisoft\Definitions\ArrayDefinition;

$definition = ArrayDefinition::fromConfig([
    MyServiceInterface::class,
    [42], 
    'propertyName' => 'value',
    'setName()' => ['Alex'],
]);

Here the unnamed first (0) parameter (string) is the name of the class.
The second unnamed (1) parameter (array, if present) is the constructor arguments.

Raise an error if unnamed parameters are present along with "__construct()" or "class".

Also, the '$' notation here may be redundant and looks like a developer error visually (lack of double quotes).

This improvement will simplify development and reduce the amount of code.

@vjik
Copy link
Member

vjik commented Mar 17, 2024

In generally, idea with alternative syntax for class name and constructor arguments looks good. Seems, there should be no technical problems in implementation.

✔️ more shortly
❌ less clear
❌ two ways with same result


Also, the '$' notation here may be redundant and looks like a developer error visually (lack of double quotes).

Don't agree. $var looks as class property in code, it's clear and consistent with method calls (run()).

@xepozz
Copy link
Member

xepozz commented Mar 17, 2024

As an alternative:

$definition = ArrayDefinition::fromConfig([
    'constructor' => [MyServiceInterface::class, 42, 'namedArg' => 'value'], 
    '$propertyName' => 'value',
    'setName()' => ['Alex'],
]);

So definition can be initialized as the following:

$constructorArgs = $definition['constructor'];
$class = array_shift($constructorArgs);

$injector->make($class, $constructorArgs);

But I don't see any of the options as a way to optimise either reading or writing definitions.

@terabytesoftw
Copy link
Member

In my opinion, as this constructor and class are, they are excellent and explicit, having several ways to configure the same thing could cause problems in the future and confusion, the ideal is to have a single syntax.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants