-
Notifications
You must be signed in to change notification settings - Fork 67
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
Question: How to handle instances of the same class with different purpose? #133
Comments
It seems named instances do the job just fine: https://r.je/dice.html#example3-6 👍 |
Yes, named instances are the best solution for this. It's basically what you have in approach 2, but you'd also create a named instance for |
Hi, thanks for the reply. The following code snippet contains my solution: // Auth
$this->rules['Backbone\User\Auth'] = array(
'shared' => true,
'constructParams' => array(
array('instance' => 'Saft\Rdf\CommonNamespaces'),
array('instance' => 'Saft\Rdf\RdfHelpers'),
array('instance' => '_Session'),
array('instance' => '_RequestCookies'),
array('instance' => '_ResponseCookies'),
array('instance' => 'Knorke\ResourceGuyHelper'),
array('instance' => 'Backbone\User\User')
)
);
// ParameterBag instance representing request cookies
$this->rules['_RequestCookies'] = array(
'shared' => true,
'instanceOf' => 'Symfony\Component\HttpFoundation\ParameterBag'
);
// entries are of type Cookie and are subject to be send in Response
$this->rules['_ResponseCookies'] = array(
'shared' => true,
'instanceOf' => 'Symfony\Component\HttpFoundation\ParameterBag'
);
// Session
$this->rules['_Session'] = array(
'shared' => true,
'instanceOf' => 'Symfony\Component\HttpFoundation\Session\Session',
); |
Now, all we need to add to make this easier is the ability to specify constructor params by name and not solely argument position. |
I have the following example code:
The class is about HTTP handling, basically. The constructor receives two instances of the same class, each with its own purpose. There are other classes which also require and work with $requestCookies and $responseCookies.
I want these two to be unique and be reuseable, because i am working with a MVC approach and handle cookies on different levels (e.g. Controller, Auth). After they were created, they HAVE to be reused. Btw, i am strictly against usage of super globals or singleton variables.
Approach 1
Create dummy subclasses of ParameterBag: RequestCookies and ResponseCookies, which extend ParameterBag basically.
This would bloat my class base, furthermore these dummy subclasses would have no value except being part of a workaround.
The according code would be:
Approach 2
Write a custom value for
instanceOf
to force using a certain instance, like:The problem with this approach is, that it "calls" dice immediately, therefore real instances get created. That is problematic if i am in a test environment and what to injection further/different rules before.
Which rule(s) do i have to apply, so that i get instances which are shareable/reusable?
Help is much appreciated
Thanks
The text was updated successfully, but these errors were encountered: