Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

How can i call an instance of my model class from the DI? Can i?

Is there a way to avoid wording like $model = new ModelName() all the time? Can i call my models from Dependency Injector without actually creating a new instance like mentioned above?

First of all - for what you need new instance ? If you are doing new ModelName() then it means you need to save it then, then yes - you have to call it every time, but if you need to select some model of this class there are modelsManager/ModelName::find/ModelName::findFirst/ModelName::query



7.3k
edited Apr '16

Why we don't create an instance for View for example, we just call it $this->view and it works. My models are already in bootstrap, as well as View, why i can't call them like view, request, etc classes? Or i can? Then how i can do that? :)



107.3k
Accepted
answer

But why you want to do that ? $this->view is just one object, you never creating new instance of it. Why you need only one instance of your class in di ? I don't get it.



7.3k

uh, i did not know there is always only one object in di. :( So, there is no way to get a pool of objects that can be instantiated as new objects?

edited Apr '16

But why you need it ? If you need object of some class you do new Class() like in any normal language. Just tell me why you need this behaviour and we maybe can figure something out. To create new object use new Class(), i don't see any logical behaviour for creating new instance of model calling di, no sense in it.



7.3k

it was just a thought of how i can get rid of many new Class() in my code

edited Apr '16

But why you want get rid of it ? What is your use case ? When you are using it ? For what ? Etc etc.

If you need to create for example new user and create it in database - then you should aways use new SomeClass(). If you mean something else - then tell me what.



7.3k

Doing some refactoring and thought i can use new instances for my models from di and without writing new Class() to have more static look of the code. Sorry, nothing special behind the curtain :) Thank you very much for you help. I learned a lot!

edited Apr '16

It's really bad behaviour and you should NEVER do it. new Class() give you stubs from IDE, you can jump to declaration etc etc. What you want won't be user friendly, and also you would make sure you always have this service setted in di. Also just for instance of course you can do it:

$di->setShared('somename',function(){
    $user = new User();
    return $user;
});

and then in code:

$di->getShared('somename')

But it's really, really, really bad and you shouldn't use it. Di is for storing services etc, not for creating object instances, especially not for creating model instances.

You can do it perhaps in ControllerBase if your child controllers extend one main parent controller.



7.3k

Di is for storing services etc, not for creating object instances Thank you, Wojciech! There has been my misconception about DI. I thought, it is used to create object instances. Understood a lot from you today!

Well, when you access something rom di it's calling function which can create object instance and return it. But it's it shouldn't be used for creating model objects, or just any object.