 on the back, okay good so okay so my talk today is about the dependency injection and I want to introduce about me first I'm Michael Bui I started working with PHP a very long time ago and obviously working for companies in 2008 I like programming I like learning new things and I like building things also like photography chess, traveling, tracking, sports, I like food so every week and if anyone want to join us you are welcome and I'm the father of a lot of daughter okay so today is workshop and I repair one like exercise for us for today so if you go to our pp.com.htr website we will see the list of speakers right so our task today is like we try to pass the website and get the list of the speaker from that it's very simple task only yeah so this is a tool we will use to help us on this one and before going to the workshop I expected everybody to I get our hands dirty try to write some code try to run it and to see the result and you can feel so let me go through the website first so this is the pp.com.htr website and I want to get on the speaker if you go to speaker tab here you will be scorned out to the this section and what I want is I get all of this name even further down and I want to sort it by alphabetical orders so okay let's assume we can anybody can see it clearly or on the screen okay so amazing this is interface of class I want to have a conference time classes we have one method called list speaker and this list speaker will return an array of speaker names okay so anybody have questions so far okay good we are good to go the first thing I prepare the this so this is a basic code for us to start and I will share this link on our event page is here so you guys just go to our Facebook and click on this link you will get the list of the code okay so do you have a composer installed on your machine already okay so if you don't you just go get composer.org and this is how you install composer on your machine so I give you some time to install composer and if you have any difficulty to install it you find any issue or problem feel free to let me know I will come to help okay you don't really need docker yeah yeah you just install pps you know more because this one is very simple one that means I go into my yeah that means as a Windows native yes if they have I'm not sure Windows 10 have it okay I never try in Windows 10 okay but the link is get composer.com also prepare your folder structure like this like this workshop folder and so we're in the same line and we can start easily okay okay okay so composer.com file you can put anywhere where you can access yeah anywhere on your local system yeah but usually you can put it the same same folder with composer.json yeah you can use it okay give me a minute yes this link download and run composer.json no because this is Mac this window okay do you copy it like this yeah you follow this one yeah and copy and run in your terminal yeah okay keep some time to download let's click next next pps version here right okay it's installing see finish already where's your IDE IDE I'm not sure which IDE are you like she's busy with somebody or somebody has a question can you help him to install the composers I think yeah so anyone okay yes yes can you see my folder structure okay I don't know how to zoom it in but let me try okay give me a second sorry I cannot zoom out yeah yes sorry no you don't need for my talk maybe I'm not sure what the other but for my talk you don't need to install just run in the native p8 piece okay I'm not sure you ask yeah yeah sorry currently I tested in pp 5.5 so any above and by 5.5 should be okay yeah sorry json extension because okay so when you install pp you didn't install the json extension so I think you can use them I mean have pp but then you have to find where the pp run run time files okay and then I can yeah yes I think because here you can yeah you are using 16 right so that's why they cannot find any version so if you install zam and I think it's just be able to fight but it would be easier if I'll just install right in my DC and then do the yeah but I think senior is already installed sequins you can just install the pp json yes yes yes just so for 16 pp json installation on Google yeah yet it should be able to run in window 7 as well I'm not sure maybe you do you have permission to install okay okay let's see what happened to see if the have composer installed so you guys can see the photo structure better now yeah can this plus mean anything about this yeah just only on Facebook so everybody can check it out message let's see okay because in pp computer configuration this function is disabled yes yes you can okay let me try for you usually you can just be pp that I for info and you graph dot and I here it should be in the first year this is a lot yeah yeah sorry yes after you when you install the composer dot far I don't have I just don't want to be okay you you have a structure right and then you download the composer get composer the off okay where is that okay do you know where is this look at let me see okay this is the right composer okay we have here it's have a cushion so okay so where is this project so that's top that's top of the ground okay good so CT project and this photo is workshop okay then you can run okay let me check if I should have pp now it's been past the composer rotation fine and one is download the version even get the dot log fine the dot log file is locked the version to his packages yeah so it's more convenient for you to download in the next time okay welcome so whoever downloaded and have composer install can run this comment and you can change this part to your local part to the composer dot far so after you run this comment to install on the dependencies for our projects and this command will generate the composer dot log file here so you don't have to worry about composer dot log for now so just run it and it will pass the composer rotation fine and download on the packet we define in the composer rotation so this is a command because I put in the many on the parents folder yeah yeah you can change this to your local part yeah I think by default it will be inside your user folder if you download the composer dot far thank you so we can't really see what's going on so basically we have already put in the downloader yeah and then we have also installed the composer yeah so what's the next thing that okay you need to follow mark the folder structure give me a second first I think it's okay you can run like this where is your combo okay I think composer is inside here okay your local so where is your project folder my project folder is in my Zen folder okay oh I have to actually write in my yes all right so I think if I can copy this part so you can run the composer is strong because it's already have a composer position here I will you change this structure later okay I just run it you don't have it and you install first give it some time yeah after it's in stone gate and it can run the composers yeah or if you're not running you can just really run the combat yeah this one and then I copy the composer application file in this folder and then also try this one yeah how did you install the ppjization actually oh no this is in the Sam is it a Sam but this one is a sick win they are two different things yeah so like for example pp.i so you see you don't have any Jason library in store yeah so you need to install for six win not for Sam so forget some for now yeah yeah okay yes correct but this part where's your composite of far yeah that's one okay it's a different between map and window yeah so basically this is copy this how to copy here so this is a part your local part it's running fine now okay let me check where how do you install ppj yeah okay where where is your composer rotation fine it's in this directory no this is it different sorry can you see I have a composer rotation yeah no this one don't have a composer rotation you need to generate the composer rotation if you go you have a Facebook here yeah yes I set a link here you still can see I think or maybe each I look in first now this is a system and have you tried installs this pp my friend Jason no no okay you don't have a assist so I'm not sure is the solution where is your six win where you run the six win to install no this is a fine already but where do you extract this one no no you already have a you already have it in store yes but I want to see where is a folder okay you extract the 16 folder here right I'm not sure okay then you can try this one okay just follow this one you see so you make the folder structure like this okay for your pp installation you didn't install the open SSS open SSS extension is not installed how did you install pp because it's missing this one so you cannot it it's a long time I haven't tried in windows so sorry about this one yes you just right yeah no need to run all of this you only need to run pp position only position and paper far yeah don't copy like this this is one comment so have you install up six okay good install pp json and p let's try this it's thin downloading okay so you prepare the folder structure like this also cause interface five inside the folder the conference interface fine yeah they put in this follow this for the structure this one if you install properly then yes you can just run it yes so basically let me check yes now you have json support so you can try to run the composer install it's a bit difficult to run from here okay I think you can ask her she also have window so first thing you need to install sick win yeah this is a package here I think can you have her or install the pp not yet so I think they need to download sick win have you installed the pump on the package right yeah later when everybody install it I will start yeah yes decision is not okay because where is your decision composer rotation right you see I have a composition here and having my you need to update this one first because it's outdated I just update it not self update just a bit self update that means it's updated composer version when you run the composer update you update the package version okay so now you just can compose install yes yeah okay good you are good to go everything is there so basically you make a folder structure like this okay yes yeah sorry no the vendor is here yeah the vendor is a composer folder yeah so don't you don't have to touch it you just put the component interface inside inside the folder yeah it's been setting up yeah okay what is the error you see I can be I can be extension okay very simple you can just run like I have sick install pp I can be and pp mb mb string then you go yes some time in another run you need to install for this will be okay install go to go okay you can go next give it some time okay give it some time to download all the packages so if you need some if you see any missing package you just do a app seek install and pp hyphen the name of the package yes yeah welcome because you are running pp 5.4 owner package now is running pp 5.5 at least yeah yeah I don't know but you have to use pp 5.5 at least which which first with my question and I'm using a big one okay then you use a better vagrant or which OS you are running Ubuntu or what this one is Ubuntu okay which Ubuntu first let me just it's not it's yum install so it's okay I am yes so this fragrance why don't you just install which of your and your local I think you can run it on your local because from here like this can I close this one as well so your local is already being fined fine you can create any folder yeah so you have composite install right yes so you can start thanks to Jason here yeah you know save me okay so you try to figure out how to get the speaker from the website at least of speaker from the website okay yeah yeah so all you need to put the folder structure like this first so let's have in the source and conference and go to conference interface this fine yeah yeah I mean this one is thin in this one is thin in I mean the folder structure the conference interface should be inside the conference folder okay and the conference from the inside the source from the okay yeah so you have a local source in one home conference yes okay yeah just read yeah because this one later is have a some tablet pp standard yeah so one folder inside the soft folder will be a tablet sub namespace yeah yeah is this no you need to install the composer just go to the getcomposer.org no no no no no no website yeah getcomposer.org yeah getcomposer copy and paste this for lice own for lice own on the yeah yeah let me have it yeah and just spaces and the people sometimes too okay it's downloading as anyone is still have composer install okay okay who hasn't installed who hasn't installed composer successfully one two three four five I think own from windows right now it's available here let me see where is this fashion deal you need to specify the directory as well they should have it and it's just extension here yes so basically yes you need to remove this one and let's see install just leave it so far so good right okay it's thin installing because this computer is very weak I mean when it's when you download too many it may cause some conflict or some error yeah so that's why this computer is too small for that usually this one is usually this type of computer you for using for like watching movie browsing website writing some document but it's not for programming yeah this is like a mini computer rather than a real computer not really if you if you want to do the program do a programming you need to get a map highly recommend it because the terminal image is much better yeah and actually the mask they have you have a balance between the graphic in the face and the terminal as well yes you need in Linux right you don't have a graphic the good graphic in the face but in Mac you have a board okay sure so how is this okay wait wait wait yeah okay I think I can you open your terminal okay let's see it's not installed yes ask her ask her how to install app 6 yeah no not this one up sick is another one yeah ask her it should be a very simple comment sorry no you don't need a bunch yeah actually composer is a pp script composer is a pp script so basically you just install pp okay where where you run the command for be a insta composer so is where I run where is a composer but I mean where is a composer the farm I think honey honey where is a composer the fire in window see drive at data actually it depends like one like one of them installed there right it seems he's inside his user folder okay so it really depends on where they run that command okay no but the thing is they run those as a epic as a somewhere I need to run the Composer install no you also have you you have a six-win as well right you install pp by Sam right I try to download the when the installer is corrupted so I just stick to the and then when I after I install the talker I find that my machine always something wrong oh yeah it's just slow okay so where is your composer rotation have you create have you create drum composer rotation fine okay you go to our Facebook okay you go to the our event base I will see the script you just follow okay click on events up here right here happening now you're in oh yeah right you run up get up sick from here this line okay no no not not this doesn't have anything no it don't have sorry okay guys it's time to start now we take a very long time to install on the dependencies so okay so this is a interface and all the our library we base on this control phase interface to get the data from the website so this is a website and I want to get owner this name so anyone have success in how to get this name yes but it's very complex to pass for example if you you know jQuery right usually I want to hike this website sorry but I'm tired here to make sure that we have jQuery yeah the dollar sign dollar sign is very famous for jQuery library so then I will try to see I think it's very difficult for people to see this one yeah yeah so okay so basically when I see when I try to inject the web page I will see that the name inside the H4 tag cannot make a bigger fall from here yeah ah yes yes yes good good good good can you see now can you see from the back okay so basically this is a web inspector from Chrome I think in Firefox we have a five up in Safari you can have a same as well it's also a Chrome web inspector you click on the this button this tune so you mouse over on the element it will show you the structure and from here we can see that the structure the name is inside H4 tag and H4 tags inside the offset L1 class so I try to test to make sure that this selector will get all the name yeah so see so basically I use a I find the class offset L1 and inside the offset L1 I find the tag H4 and here we can we see we see a list of array here so basically maybe this is not fully correct but we can have a concept that this one have the list of the speaker in our website so if I get the name is printed on the name one by one together so basically we can see that by using the query we can get this name easily but we are using pp so how we do that yeah so the first thing I will do I will create a class a concrete class implement this interface I call it pp asia 2016 version one let this is version one later I will have another version to show you what is different between these two so I implement the conference interface here and you see this okay I need to zoom it out can you see now okay so you see they have a red red underline the red underline when you mouse over it is say that I implement the conference interface but I didn't implement the list speaker method so now I have to install the list list speaker method okay you see the red light gone so basically once you implement an interface on the method inside the interface must be implemented inside the concrete class okay so our main function our main code will be happening here so but usually what we're doing now is we try to connect to the other websites get the data get the HTML data and pass the HTML data to get the speaker names okay what that's what we do right so basically I need to have a client to connect to the websites okay this first thing we need to connect to the website and get the content of the website the client class is what was the utility class from Gazzo package the Gazzo package is for HTTP clients to connect to to make a request to any website so once we get the data with the data of the website we need to pass this right and we have a code controller so we create a controller and we put the response inside the controller and we will use this one to find the name so similar to Zequery we will get we will use the same selector from Zequery so this is very convenient for us I think Zequery is quite famous for and we already get used to with Zequery so the note will be the list of array that we see in the web page now is in the web inspector here here is the list here is the note we have so I need to go through the list of this note and extract the names okay so for each note on the text content of the note will be the name of the speaker so I will put this on this name into the array names and now another requirement is we shot it by InfoBad right now shot is a built-in function from PHP so this one is I just use this one for from demonstration only so you don't care much how it works or what is what is this but this is function to shot by InfoBad short alphabetically and after shorting the names we return it any questions so far are you sure this one will work okay how to make sure that this one will work yes we write a test for this now so I create a folder and make sure that the folder in the test folder will be follow the structure in the source folder this will be a mapping that we are when we test a class when we have a test class the test class is for a source class okay so so this is a test class so all the names names play for test classes we start with test and after that it will be exactly like the source class and on the test classes must end it by this suffix the test suffix this one make sure your class name must be exactly same like this otherwise the ppunit will not detect it okay so in a ppunit test this setup method is running before every single test of this class and one test will be prefixed by this name test with a lower case if you don't have this step for example you want to write on name function name like this you need to indicate it in the annotation code this test so ppunit we know that ZIT is a test but for your convenience we just use a prefix and we set up the an object of the class pphcr 2006 versus one and what do you what we test now is we try to get all the names by calling the method from the conference right and here we see this guy right so I know that this guy must be inside the list so so I expected this name is inside this array of names so what are you doing now is we try to run the test to make sure this test is passed so since we already used a composer install so vendor vendor is a composer folder and this is the bin is a binary file ppunit is a library from ppunit package for you to run the test and by default this one will read the ppunit.xml which I provided before okay you see the after running the test we see that all the test paths we only have one test only so the test path mean the name this guy is inside our list so again in order to make sure that job application is running fine you need to write the test because the test is almost instant feedback for you to make sure that your application is running fine or not if you need to deploy it in order to make sure that it's running takes very long a lot of time to go back and forward so come back to this now our program is running fine so like we can say the boss I'm done but it like how okay anybody can see the problem from this code okay just example I close my Wi-Fi connection so there's no internet connection to the website and I try to run the test again boom fail because it's cannot connect and when it cannot connect there's the name array is empty that mean there's no the guy is not inside the list so just say it didn't fail but our program is running five before and our program shouldn't depend on the internet connection right how you how we make sure that the problem from the code is that when we run the test we actually run a connection with another party so in order to write the test we try to avoid that one and how to avoid that is by mocking the request so we don't actually run the request but we still return some data for for us to to run the test okay but if you're doing if you write a code like this we cannot mark anything at all because when we create when we call the this method everything inside this method will be run and it's really out of our controls there's no way for us to make change inside here and here we come with the concept of dependency injection as you can see inside the code we read this code will depends on two dependencies the first one is a TTP client the second one is a drum crawler so I will try to improve this one okay so basically I will leave this method here but I will create a new class the class is thin implement the interface and I still have implement that method public function guest speaker I think so yes no what is this least bigger sorry okay but from here okay watch okay can you speak louder which five okay okay this test fire right okay sorry you want to modify this okay you can copy it I give you for one minute to finish this one yeah okay I give everybody one minute to follow this code and run on your local to see the I sure data yes I was actually I have this on github already but yeah I want people to follow the step by step first eventually you will have it in github and you save it everybody yes so same SGP yeah yeah okay you need to go to ATP this one yes okay I come back because I don't remember symphony component right yeah so I don't guys is let's try that's all right yeah let's try to run it yeah I think component not components sorry yeah you edit yeah yeah no s save it you cannot run business this is class you cannot run this one have you rather have you write a test okay no it's not running the test like this you need to run by vendor oh be in ppunit see oh no test it's cute no you need to put this one you didn't follow the folder structure oh yeah okay I can't see that's why I said on the beginning we need to this because of the names just correct how do you store the composer this one is installing the server yeah but you mean this it cannot put the folder inside here I run as root he's a test server it's okay no I'm not sure if run it no it's usually okay let's try can you run it okay yeah it's okay why it's not output anything here but this time it's not output anything you know the unique thing right yeah try to run just run just run no because that's one is it's not it's not detected anything okay yeah yeah because there's no there's no class yeah let me see this this is okay correct already yeah okay a bit difficult to say because that's not initially when I download all the dependencies it was on the can you try to run the composer no it's okay oh no no can you make this part right table yeah try okay so is this can you run it okay good anybody here have problem running it how to run the test again once again okay just vendor been ppunit yeah vendor the top yeah been ppunit yeah that's all just run it okay like let me check let me check you don't have a pp exam so you need put on the root folder oh yeah just drag it to the root folder yes okay now you can try to run it okay like 13 no comma semi comma yeah no in the source file did a test file so it should be here see no comma okay can't run you know like 10 son is still more like 9 can you go back can you go back you know okay you need to use import class give me angle back to the use statement castle line yes symphony component Dom Kronler Kronler yeah yeah just run it there you go okay no no no you go to this one yeah go to this yes and you copy the whole line open on the web browser even any web browser can do okay and then if we ask you to look in and we display a token for you you copy that token here no cannot you have to it because for security reason yeah I think because many people here download from the gift just it's similar like a recapture yes correct what is this have you tried to install the Composer package this one window I am not sure this is the first test this one okay good can you follow okay you guys can follow it can run the test right I cannot run the test okay test excuse test excuse that's so strange so strange right do you want to sit no it's okay quite strange yeah currently the topic is testing no dependency injection yes yeah but this one is the testing to make sure the code is running fine first yes after that I'll tell you some more yes we are going to we're going to eat now I don't really understand why yes so basically are you edit from here right okay and I'm using kp storm source 24 don't need this yeah you got welcome this one I'm not sure you have to install how do you how you install the sequin can you run the sequin installation again I think haven't installed because it is to be in no I mean no I install the sequin not this one from here right how do you install it I'm not sure previously it's not here so yeah try to ask the lady because I'm not sure about this but sorry are you running yes okay okay this one right first color symphony color conference where is this I think you're putting the wrong one but you need to copy this line under conference you can just run vendor okay okay cannot I think it's related to japan missions test conference I think it's good to the conference it's cannot rate it render try to run in a composite install again just remove the whole vendor okay so you run the composite install again right okay great no it should be the same see it's still running not getting anything no this one is because you run pdp not sure about this composer is running fine can you have him on this one he run the pdp you need but nothing happened at all but if you run hyphen version display something so that means the library is still running actually do this log file right because you run pdp and then inside vendor hyphen version try have pdp pdp nothing it should display something even if the code there is something wrong with code you still display something right no I think this line is wrong in array so have a name let me try to help you how do you say how to escape escape sorry use a name because of that syntax doesn't display anything at all but you have some idea already somewhere else this one is now damn fast explain test content I think there is something wrong with the code so there is some error syntax just now damn fast body it still display something already still have some error but everything look fine copy from me we don't have any other file but there is another one in source of 2 files okay done probably I think it is a syntax issue but if you run it is not for any test maybe it is some permission issue so basically if you put a specific file it can run so leave it like this first ah no need I just have to have the full path so that means I need the full path just leave it for now first I think you haven't installed SSL try to how to install okay guys let's continue we have a problem with our test what we are doing is we find a way to mark the connection so basically the next version I call this version 2 I will do a different with the previous version in previous version we need client ATTP client and also the DOM crawler now I put this dependency in the constructor of the class can see now basically I put this 2 dependency in the constructor so later if I initiate the object of this class I can pass in the client and the crawler as a dependency and before passing I can mark the object mark the ATTP client so we don't have to send the actual request over internet at all I will install these 2 objects as a protected property of the class so I can use later next I will copy this code from version 1 to version 2 and I will modify it so from here we already have a client and crawler we don't need to initiate the object inside this method any more so now we will write the test for this one to make sure that this change work if I am going too fast you cannot feel free to interrupt me so you guys have some time to follow it now what I am doing is I create the test for this file for version 2 so the structure of this test we have a conference object as a protected object in order to reuse this we have a setup to initiate the object of the class and we have a test to actually run the test for our code so I will write the test first and we will come up with own the dependency later so basically what we want to test is make sure that this name is inside the array and here we see that the name is not defined so we will get the result by calling the conference method it's good for now but the conference object is not initiated yet we need to do it in the setup method so in order to initiate the we need to pass in the client and the crawler object so basically what we are doing now is like we initiate the object outside of our class and pass in the object into the constructor I guess this one will work and I am going to run the test now to see if it actually work or not yes both test version 1 or version 2 are working but let's see if I turn off the Wi-Fi what happen both fail you see we have 2 errors here so actually the version 2 haven't made any changes what I am doing now is I will mock the HTTP connection so we don't rely on the internet anymore so PEP unit give us the ability to mock object of a class by using this method get mock builder and we pass in the class name inside and we want to mock only method request why is only method request because inside our code this is the client only code a request and this request will return a response object so once I mock the request object request method like this I need to this is just declaration that I want to mock but I need to write the mock explicitly so client so I expected the request method will run once on the client object and it will return the mock response object but we haven't defied the mock response object yes so we need to defy it before this client here we have 2 method need to be mock the first one is get body and the second one is get contents because I know that the response object need to have some data in the constructor but I don't want to create the data of that so I just ignore the construction the mocking is still running fine since we defy 2 method to mock here we need to declare the mock for these 2 methods so here is the thing happen we don't actually get the content of the page but we know that we want to look for the hash for tag inside the object L1 so we will build a structure similar like this so what I'm doing now is I build a structure html structure I push this guy inside so what this do is like instead of running an actual request to a html server it will return a mock data and our code will run on top of mock data so since this client is a mock it's better if we rename it as mock client instead of client so now instead of passing into the constructor actual client we pass the mock client so when we code this method this one we run this line and this line instead of actually connect to this html it doesn't connect to anything at all but it still return a data for the below code to be run the data is defined here and our code will run on top of this data the crawler will try to find own the name inside the hash for tag inside the object L1 class I guess it will work and let's try you see my wifi connection is still off so what I guess is one test fail one test pass the first one you fail the second one we pass we have two test one error and you see all the error is coming from the first one because there is no connection so if I enable the connection now both test will run and pass so by moving the object inside the method body to the constructor on the dependency to the constructor we abstract the dependency and later we can inject it whatever we want it make our code is loosely coupled and also testable I think I need some more time to talk because we haven't stopped here yet for example this is library but when we go to the website we type the URL on the website the URL will go to the router and the router will find what is controller and what is action for that one the router will be run the controller class will be initiated and then it will pass the dependency into the controller on another framework is done already but we don't know what is happened behind and where is the dependency is stored in the framework so the one who stores the dependency in the framework the code dependency injection containers or DI containers from here I don't actually write the routing but I write a very simple script to dynamically map the request to the controller I create a new file called index one index one we use the version one I will show you the two ways for inject the dependency into the controller the first thing I need to include the autoloader file so on the file we download before we will be used and can be used in our system I need to increase what I'm going to do is I'm going to write some scripts so I can run something like this pp index one dot pp and I put the conference one and action is speaker I will run something like this later and we need to get two arguments in order to run our controller just forget this one first first argument is here indicate the controller class the second one is indicate the controller action and once we get the controller class we initiate the object of that controller and we call the action like this so basically this is very simple mapping with our code to controller but the thing is we haven't write any controller class yet we only write the conference class which is library only now I'm starting write the controller to serve the request I create a directory called controller this one is namespace as well inside controller my common bare practice is like I have an abstract class or I have a class interface so all the there will be a if we have a common things between the class we can put in the abstract class to share the code so we don't have to repeat and rewrite the code anymore this abstract this abstract class do nothing at all because we don't have any common we don't have any share or maybe we won't have but just leave it like this because like our application is not so simple like this for the long run it will have a common code I will write the first controller I call it conference once and this one will extend the abstract controller we just created it few seconds ago and inside here we have a method called speakers so here we have here and once we run this command we try to map this request to our controller so from here I just output something like I return pp for example I put here fuba is very common things and I expected I will get this string output to our my console our controller conference one not found ok you see the error is like the class is not found because our autoloader this file is created after our autoloader file is created so we need to recreate the autoloader file in order to do that we can run the composer install again it will not download again but it just recreate the autoloader file it's just regenerating the autoloader files our controller one is size ok yes correct correct my type of sorry the string is outputted so our routing is working it's very simple routing but here we want to get actually the name so what do we do here we initiate the conference and we call the method list speaker and then we expected we have a list of speakers as we seen before see it's outputted here so everything is working fine but already says we don't want to have this we don't want to have a construct the initiation of the object inside our code so we wanted to move it to our constructor so I will create a constructor here I will create a protected property to store the constructor object and from here I just reuse it everything look fine here and do you think it will run I will try no sorry because in order in our routing we initiate the controller and why initiating the controller we don't pass any agreement to the constructor that's why argument one none given so how you given it one of the thing is like we automated the process because a simple thing here in the index file we can initiate the conference class here and put passes in the thing is like how about another controller the argument is very dynamically we cannot put everything here so what we doing is we use the reflection class to get on the argument and for each argument we initiate the object for the argument and we pass it into the controller and create a right dependency to store on the dependency of this class for each parameter I will get the class name for it and then I will initiate the object for this parameter and I put it in the dependency array and now we cannot use something like this anymore we will use the reflection class to initiate the object for you so this method we create a new instance with the arguments the argument will pass into the constructor and the argument is arrays as we have here so this one we try dynamically find out what is the parameter of the constructor for this class and we automatically create the object for this class and automatically pass that parameter into the controller construction I guess it should work here now it should work let's see so later if we have more dependency we can automatically inject into our controller we can modify our routing but this one has limitation the parameter class should not have any parameter as well so if this one has another parameter and the parameter requires another parameter we have a graph of dependencies so there is some library already written for that I am not writing here but if you want I can recommend later just contact me I will let you know but this is a very simple way to demonstrate how you can automatically inject the dependency into our controller and then form our controller so you can have fully do whatever we want the last one I want to introduce is version 2 of this method how we inject the controller as how we inject the dependency into our controller I will run similar like this but I will create another file called index2 another controller called conference2 with the same action what I am doing is exactly almost the same I copy this file first from here I have a class name this one I will use the DI container to inject the dependency into our controller from here I register a conference and this container we manage on the dependency and we will get dependency dynamically in our code from now I will create a new version of controller exactly like before instead of I store the conference I will store the dependency here inside here and inside the action I will try to get my dependency from the container so this is the ID of the dependency this ID have to match the ID we defined before inside here and from here we will use the dependency we just get okay but let's run this one first to see if it works I guess no but I need to create a controller object here and the controller need to have a container as the parameter for constructor so I pass the container here yeah so let's run it okay here we come to another issue the issue here is we create the conference object already but our conference object requires two parameter client and controller but client we haven't passed the client or controller to this class so that's why this one not given that's why it's true it's wrong now we need to define the dependency and pass into the conference class we can define inside here we register a client and we register the controller and now we add argument to this and we add the second argument for controller now this one should work because own dependency is resolved so this is how DI container works we register own our dependency inside the container and it up to our controller to get whatever it need so by doing like this you don't have to specify the dependency in the constructor of the controller because who know later if we have a new method we need another dependency and some controller have like 10 or 20 method and we have 10 or 20 dependency or even more the constructor will be very big so by using the container the dependency of this controller is only the container and it's method it action it will get the dependency as it needed from the container so by using like this the code will look very cleaner in some framework like Laravel or symphony for Laravel you can define own list of dependency inside I'm not sure it's the provider app service provider and in symphony you can define all of this dependency in the config also service.jamlify I put it here because I just want to make it very simple for us to follow what is the magic inside the framework how they map the URL to our controller the basic thing is like this we will have a more thing like authentication login or catching or something like that but basically it's like this I think I'm done for the dependency injection now anybody have a question to recap this one when you write a code think about the dependency think about how you organize the dependency to inject the dependency into our class you can do it automatically like what Laravel is doing I like it somehow but sometimes I prefer the symphony way to have a container so I just pass the container inside the controller I can use whatever I want I don't have to declare multiple dependencies in my method it have a cross and con but remember try to follow the dependency injection and one more thing you can improve from here is like instead of depends on concreted class you can depend on interface but the thing is like it's not easy to initiate the object for interface here because you cannot create object for interface there is a way to do that by you can do alias like you map for this round you map the conference interface with a concreted class for version 1 or version 2 it's up to you but it's a bit complex so I don't want to introduce it here and by using the dependency we can easily swap the object we can easily test it by making our class decoupling to each other we can easily extend it add more features and we replace for example instead of you don't have to worry about if you use Bicycle or Mongo or Postgres DB you just write abstract class for that or interface for that first and we inject it later so do think about the dependency injection before you start writing your code thank you for listening