Skip to content

Optimizing constructor and argument initialization #91

@phphleb

Description

@phphleb

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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions