How to make your own PHP IoC/ DIC based on how Laravel works?












2















I am interested in writing my own IoC/ DIC for PHP based on the way Laravel works. This is purely for learning as it interests me and I want to understand the concept better.



I've been reading the Laravel documentation for Service Containers and Laravel IoC. I understand the concept and why dependency injection is good practice but what I am failing to grasp is how such functionality is coded.



Take this example from Laravel:



class UserController extends Controller
{
/**
* The user repository implementation.
*
* @var UserRepository
*/
protected $users;

/**
* Create a new controller instance.
*
* @param UserRepository $users
* @return void
*/
public function __construct(UserRepository $users)
{
$this->users = $users;
}
}


My question is how does UserRepository get injected code wise on the PHP side? How does the Laravel framework operate in the background on this? I read that Laravel may use the reflection class in PHP to achieve this, but I am unsure?



I also understand there are also a few different ways of doing dependency injection such as using setter methods or resolving classes using a static function? I am adware of and found useful the Pimple Project to aid in my understanding but I am more after how the Laravel way works with the automatic class injection, could someone give me a quick example I can go off and build from? Because with the way Laravel works and its DIC, there is no call for UserRepository via a function, it just gets injected? - That is what I am interested in, how I can code that myself.



I read also that a proper DIC would allow you to write configs for each class in different ways, such as XML or in PHP code, a way to setup the class instance before being injected, is this correct?



And is there any difference between IoC(Inversion of Control) and DIC(Dependency Injection Container)?



Also, what is the difference between Laravel Service Container and IoC in this case?










share|improve this question


















  • 2





    This is pretty broad. This would be better for a teacher or mentor, or at the very least a discussion forum. Not really a great fit for a Q&A site like Stackoverflow. With that said, Laravel and most container implementations are open source, you could read through the code. Laravel does use reflection and then resolves the type hinted classes from the container.

    – Devon
    Nov 20 '18 at 15:16








  • 1





    You should certainly take a look at the concept of Pure DI.

    – Steven
    Nov 20 '18 at 18:22











  • @Devon I understand that there is a lot to my question but I think the main thing is, I am interested in how the injection works for UserRepository in the end :) There is a similar question here which is quite long but for me it doesn't touch on the auto DI like Laravel does.

    – tty2018
    Nov 21 '18 at 6:30






  • 1





    If you look at reflection, I think you'll find it isn't that complicated. It uses reflection to get the class name, then resolves that class name from the container and provides it as a parameter to the method or constructor.

    – Devon
    Nov 21 '18 at 13:56


















2















I am interested in writing my own IoC/ DIC for PHP based on the way Laravel works. This is purely for learning as it interests me and I want to understand the concept better.



I've been reading the Laravel documentation for Service Containers and Laravel IoC. I understand the concept and why dependency injection is good practice but what I am failing to grasp is how such functionality is coded.



Take this example from Laravel:



class UserController extends Controller
{
/**
* The user repository implementation.
*
* @var UserRepository
*/
protected $users;

/**
* Create a new controller instance.
*
* @param UserRepository $users
* @return void
*/
public function __construct(UserRepository $users)
{
$this->users = $users;
}
}


My question is how does UserRepository get injected code wise on the PHP side? How does the Laravel framework operate in the background on this? I read that Laravel may use the reflection class in PHP to achieve this, but I am unsure?



I also understand there are also a few different ways of doing dependency injection such as using setter methods or resolving classes using a static function? I am adware of and found useful the Pimple Project to aid in my understanding but I am more after how the Laravel way works with the automatic class injection, could someone give me a quick example I can go off and build from? Because with the way Laravel works and its DIC, there is no call for UserRepository via a function, it just gets injected? - That is what I am interested in, how I can code that myself.



I read also that a proper DIC would allow you to write configs for each class in different ways, such as XML or in PHP code, a way to setup the class instance before being injected, is this correct?



And is there any difference between IoC(Inversion of Control) and DIC(Dependency Injection Container)?



Also, what is the difference between Laravel Service Container and IoC in this case?










share|improve this question


















  • 2





    This is pretty broad. This would be better for a teacher or mentor, or at the very least a discussion forum. Not really a great fit for a Q&A site like Stackoverflow. With that said, Laravel and most container implementations are open source, you could read through the code. Laravel does use reflection and then resolves the type hinted classes from the container.

    – Devon
    Nov 20 '18 at 15:16








  • 1





    You should certainly take a look at the concept of Pure DI.

    – Steven
    Nov 20 '18 at 18:22











  • @Devon I understand that there is a lot to my question but I think the main thing is, I am interested in how the injection works for UserRepository in the end :) There is a similar question here which is quite long but for me it doesn't touch on the auto DI like Laravel does.

    – tty2018
    Nov 21 '18 at 6:30






  • 1





    If you look at reflection, I think you'll find it isn't that complicated. It uses reflection to get the class name, then resolves that class name from the container and provides it as a parameter to the method or constructor.

    – Devon
    Nov 21 '18 at 13:56
















2












2








2








I am interested in writing my own IoC/ DIC for PHP based on the way Laravel works. This is purely for learning as it interests me and I want to understand the concept better.



I've been reading the Laravel documentation for Service Containers and Laravel IoC. I understand the concept and why dependency injection is good practice but what I am failing to grasp is how such functionality is coded.



Take this example from Laravel:



class UserController extends Controller
{
/**
* The user repository implementation.
*
* @var UserRepository
*/
protected $users;

/**
* Create a new controller instance.
*
* @param UserRepository $users
* @return void
*/
public function __construct(UserRepository $users)
{
$this->users = $users;
}
}


My question is how does UserRepository get injected code wise on the PHP side? How does the Laravel framework operate in the background on this? I read that Laravel may use the reflection class in PHP to achieve this, but I am unsure?



I also understand there are also a few different ways of doing dependency injection such as using setter methods or resolving classes using a static function? I am adware of and found useful the Pimple Project to aid in my understanding but I am more after how the Laravel way works with the automatic class injection, could someone give me a quick example I can go off and build from? Because with the way Laravel works and its DIC, there is no call for UserRepository via a function, it just gets injected? - That is what I am interested in, how I can code that myself.



I read also that a proper DIC would allow you to write configs for each class in different ways, such as XML or in PHP code, a way to setup the class instance before being injected, is this correct?



And is there any difference between IoC(Inversion of Control) and DIC(Dependency Injection Container)?



Also, what is the difference between Laravel Service Container and IoC in this case?










share|improve this question














I am interested in writing my own IoC/ DIC for PHP based on the way Laravel works. This is purely for learning as it interests me and I want to understand the concept better.



I've been reading the Laravel documentation for Service Containers and Laravel IoC. I understand the concept and why dependency injection is good practice but what I am failing to grasp is how such functionality is coded.



Take this example from Laravel:



class UserController extends Controller
{
/**
* The user repository implementation.
*
* @var UserRepository
*/
protected $users;

/**
* Create a new controller instance.
*
* @param UserRepository $users
* @return void
*/
public function __construct(UserRepository $users)
{
$this->users = $users;
}
}


My question is how does UserRepository get injected code wise on the PHP side? How does the Laravel framework operate in the background on this? I read that Laravel may use the reflection class in PHP to achieve this, but I am unsure?



I also understand there are also a few different ways of doing dependency injection such as using setter methods or resolving classes using a static function? I am adware of and found useful the Pimple Project to aid in my understanding but I am more after how the Laravel way works with the automatic class injection, could someone give me a quick example I can go off and build from? Because with the way Laravel works and its DIC, there is no call for UserRepository via a function, it just gets injected? - That is what I am interested in, how I can code that myself.



I read also that a proper DIC would allow you to write configs for each class in different ways, such as XML or in PHP code, a way to setup the class instance before being injected, is this correct?



And is there any difference between IoC(Inversion of Control) and DIC(Dependency Injection Container)?



Also, what is the difference between Laravel Service Container and IoC in this case?







php laravel dependency-injection






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 20 '18 at 14:59









tty2018tty2018

512




512








  • 2





    This is pretty broad. This would be better for a teacher or mentor, or at the very least a discussion forum. Not really a great fit for a Q&A site like Stackoverflow. With that said, Laravel and most container implementations are open source, you could read through the code. Laravel does use reflection and then resolves the type hinted classes from the container.

    – Devon
    Nov 20 '18 at 15:16








  • 1





    You should certainly take a look at the concept of Pure DI.

    – Steven
    Nov 20 '18 at 18:22











  • @Devon I understand that there is a lot to my question but I think the main thing is, I am interested in how the injection works for UserRepository in the end :) There is a similar question here which is quite long but for me it doesn't touch on the auto DI like Laravel does.

    – tty2018
    Nov 21 '18 at 6:30






  • 1





    If you look at reflection, I think you'll find it isn't that complicated. It uses reflection to get the class name, then resolves that class name from the container and provides it as a parameter to the method or constructor.

    – Devon
    Nov 21 '18 at 13:56
















  • 2





    This is pretty broad. This would be better for a teacher or mentor, or at the very least a discussion forum. Not really a great fit for a Q&A site like Stackoverflow. With that said, Laravel and most container implementations are open source, you could read through the code. Laravel does use reflection and then resolves the type hinted classes from the container.

    – Devon
    Nov 20 '18 at 15:16








  • 1





    You should certainly take a look at the concept of Pure DI.

    – Steven
    Nov 20 '18 at 18:22











  • @Devon I understand that there is a lot to my question but I think the main thing is, I am interested in how the injection works for UserRepository in the end :) There is a similar question here which is quite long but for me it doesn't touch on the auto DI like Laravel does.

    – tty2018
    Nov 21 '18 at 6:30






  • 1





    If you look at reflection, I think you'll find it isn't that complicated. It uses reflection to get the class name, then resolves that class name from the container and provides it as a parameter to the method or constructor.

    – Devon
    Nov 21 '18 at 13:56










2




2





This is pretty broad. This would be better for a teacher or mentor, or at the very least a discussion forum. Not really a great fit for a Q&A site like Stackoverflow. With that said, Laravel and most container implementations are open source, you could read through the code. Laravel does use reflection and then resolves the type hinted classes from the container.

– Devon
Nov 20 '18 at 15:16







This is pretty broad. This would be better for a teacher or mentor, or at the very least a discussion forum. Not really a great fit for a Q&A site like Stackoverflow. With that said, Laravel and most container implementations are open source, you could read through the code. Laravel does use reflection and then resolves the type hinted classes from the container.

– Devon
Nov 20 '18 at 15:16






1




1





You should certainly take a look at the concept of Pure DI.

– Steven
Nov 20 '18 at 18:22





You should certainly take a look at the concept of Pure DI.

– Steven
Nov 20 '18 at 18:22













@Devon I understand that there is a lot to my question but I think the main thing is, I am interested in how the injection works for UserRepository in the end :) There is a similar question here which is quite long but for me it doesn't touch on the auto DI like Laravel does.

– tty2018
Nov 21 '18 at 6:30





@Devon I understand that there is a lot to my question but I think the main thing is, I am interested in how the injection works for UserRepository in the end :) There is a similar question here which is quite long but for me it doesn't touch on the auto DI like Laravel does.

– tty2018
Nov 21 '18 at 6:30




1




1





If you look at reflection, I think you'll find it isn't that complicated. It uses reflection to get the class name, then resolves that class name from the container and provides it as a parameter to the method or constructor.

– Devon
Nov 21 '18 at 13:56







If you look at reflection, I think you'll find it isn't that complicated. It uses reflection to get the class name, then resolves that class name from the container and provides it as a parameter to the method or constructor.

– Devon
Nov 21 '18 at 13:56














0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395768%2fhow-to-make-your-own-php-ioc-dic-based-on-how-laravel-works%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395768%2fhow-to-make-your-own-php-ioc-dic-based-on-how-laravel-works%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

How to change which sound is reproduced for terminal bell?

Title Spacing in Bjornstrup Chapter, Removing Chapter Number From Contents

Can I use Tabulator js library in my java Spring + Thymeleaf project?