-
-
Notifications
You must be signed in to change notification settings - Fork 13
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
Comments
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:
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
} |
As we create a concrete instance, no need to pass I'd preffer this way: $obj = $f->create(MyClass::class, userId: 5);
$obj->setUserId(10);
function create(mixed $config, mixed ...$constructor)
{
...
} |
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...) |
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. |
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); |
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: |
I don't like this way, because it requires merge constructor arguments when config also contain them. |
May be create separate method with suggested syntax? |
I don't see anything wrong with this.
Maybe |
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. |
instead of
The text was updated successfully, but these errors were encountered: