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

More comfortable way to create an object using a factory #203

Open
Tigrov opened this issue Mar 27, 2024 · 10 comments
Open

More comfortable way to create an object using a factory #203

Tigrov opened this issue Mar 27, 2024 · 10 comments

Comments

@Tigrov
Copy link
Member

Tigrov commented Mar 27, 2024

$factory->create(EngineInterface::class, 146);

instead of

$factory->create([
    'class' => EngineInterface::class,
    '__construct()' => [
        'power' => 146,
    ],
]);
@xepozz
Copy link
Member

xepozz commented Mar 29, 2024

https://github.com/yiisoft/factory/blob/master/src/Factory.php#L102

As I can see it's almost possible now to create a class with only class reference, but the rest params must be two arrays:

  1. params for constructor method
  2. the rest configuration of methods calls and etc

So I'd suggest to make it possible to use add a few options params and the merge it into another one so it can convert into the original structure:

$f->create(MyClass::class, ['userId' => 5], ['setUserId()' => [5]]);

function create(mixed $config); // before

function create(mixed $config, array $constructor = [], array $restConfig = []) // after
{
  if (is_string($config) && !empty($constructor) && !empty($restConfig)) {
    $config = ['__class' => $config, ...array_merge($restConfig, ['__construct()' => $constructor])];
  }
  ... // original function body
}

@Tigrov
Copy link
Member Author

Tigrov commented Mar 31, 2024

As we create a concrete instance, no need to pass ['setUserId()' => [5]].

I'd preffer this way:

$obj = $f->create(MyClass::class, userId: 5);
$obj->setUserId(10);

function create(mixed $config, mixed ...$constructor)
{
    ...
}

@vjik
Copy link
Member

vjik commented Mar 31, 2024

Agree with @Tigrov. For rest configuration better use PHP syntax.

The only doubt is, is there a case when the rest configuration passed as array? (I think, that no...)

@xepozz
Copy link
Member

xepozz commented Mar 31, 2024

It's not convenient to mix array- and callable- like configurations and use it together. As Factory can make necessary calls it must do it.

@Tigrov
Copy link
Member Author

Tigrov commented Mar 31, 2024

If you want to pass rest configuration, you can use as it is now

$factory->create([
    'class' => EngineInterface::class,
    '__construct()' => [
        'power' => 146,
    ],
    'setUserId()' => [5],
]);

Or

$factory->create([
    'class' => EngineInterface::class,
    'setUserId()' => [5],
], power: 146);

@xepozz
Copy link
Member

xepozz commented Mar 31, 2024

For sure. But the issue is about to make factory flexible for creating objects by instructions.

I don't like various parameters for constructors at all. It would just make the method unavailable for further modifications, as we have now: create($config) -> create($config, $constructor) -> create($config, $constructor, $rest) -> create($config, $constructor, $rest, $flags) and so on.
Let's stop propagate this way?

@vjik
Copy link
Member

vjik commented Mar 31, 2024

$factory->create([
    'class' => EngineInterface::class,
    'setUserId()' => [5],
], power: 146);

I don't like this way, because it requires merge constructor arguments when config also contain them.

@vjik
Copy link
Member

vjik commented Mar 31, 2024

May be create separate method with suggested syntax?

@Tigrov
Copy link
Member Author

Tigrov commented Apr 1, 2024

it requires merge constructor arguments when config also contain them.

I don't see anything wrong with this.

May be create separate method with suggested syntax?

Maybe

@samdark
Copy link
Member

samdark commented Apr 2, 2024

May be create separate method with suggested syntax?

Yes. That's better. When designing the syntax, we've tried mixing formats in the same call, but then dropped it completely. Merging these was creating hell.

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