2: What Is The MVC Model | MVC Model Explained | Object Oriented PHP Tutorial For Beginners | mmtuts

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so let's talk about how exactly optic currents of PHP looks like compared to proceed to a PHP and the technical side of things roughly speaking when we want to code anything using PHP we have three things we actually do when we code PHP we connect to the database or we query the database we get input from the users which means that the user does something on the website and then we need to do something with that data the user sent to us and then the last thing we do is we show things on the website to the user and now when we do talk about these three things we can do using PHP in some cases we do all three things at once for example the user goes to a website gets an input whether they need to search for a bunch of names they would then search for a bunch of names and give us some data we would then query the database and then show the list of names from the database to the use afterwards and in other cases we only do two of the different things to do using PHP for example we have a login system the user would go to lock in system we would get the data from the user and we would insert it inside the database but we wouldn't show anything to the user after wards and again in other cases who might just query the database and then show some data to the user after they're locked in inside the pages inside the website without the user actually submitting anything to us and we just sort of show it to them so now getting back to talking about procedural PHP versus object-oriented PHP when it comes to procedural PHP and we have a page you would usually do all these three things in the same place inside the code whereas if it were to go and use object-oriented PHP we actually take these three tasks and we split them into separate files and then we have different tasks categorized inside classes which we haven't talked about yet but we will talk about classes so don't worry if you don't know what classes are but we categorize these into different classes and use them independently and this is what makes it possible for us to reuse code later on because let's say we have a website where we want to show a list of names to the users inside the website then on another page we want to show the exact same list of names in that page if we were to do at the procedural way in that case you would actually inside one page we would core the database and we would show the list of names to the user then when we go to the other page that we have inside our files we would do the exact same code whereas an object-oriented we just simply link to the code that we have inside our separate files and then we link to the code inside the separate file so we just have the code in one place but we link to it instead now I do really think the best way to sort of illustrate this is to actually show what exactly happens inside the code so let's go and take a look at some code that I wrote it just basic PHP example now as you can see behind me here we do actually have a basic procedural PHP way of getting data from the user and then going into the database and then grabbing a list of names and spitting them out inside the website depending on what name the user gave us now as you can see we do all these three things that I mentioned in the same place this means that we at the top you actually connects to the database we then get the data from the user we query the database and then we showed a list of names to the user we do all of this in the same place which means that the next time you want to do this on a separate page we have to do this exact same block of code one more time in that other page and that's just duplicating the code and now suddenly we have more code on the website then in reality we should really have in this other example here you can see that we have a object-oriented example inside the page you can see with you very little huh that's not a lot of code going on in here so the essential thing is happening here is that we have all these different things that were doing like I said in separate files we have what is called a controller file which is what actually controls the input do we get from the user so when a user goes to a website and feeds us and named that they want to court a database for we handle that using one file or one class because there's one class inside one file then when it comes to actually connecting to the database we have a separate file for that called the model the model does nothing except connecting to the database and querying the database and we can reuse this code as many times as you want meaning that if I want to query for the list of names in multiple places inside my website I can use the exact same code from the model which is kind of nice and then lastly we have a view page or a view file which is what actually goes in graphs the data from the database and shows it to the user inside the website so we have these three separate files and really what I want to try and do this episode here is kind of get the point across because I know that when you get into object-oriented programming for the first time it can be a little bit difficult getting into the concept of having separate files to do separate things but trust me when you actually learn it this is going to be your preferred way of doing things now having talked about how Optik go into PHP works let's actually talk about design patterns when it comes to optical into programming in any language there's design patterns in PHP the most popular one is the one called the MVC model which stands for Model View control it ha that's the same names that the files have just mentioned now before we get into talking about the MVC model at least in a more technical aspect let me go ahead and tell you a story inside a company we have three different people working we have one guy who's the CEO he knows everything there is to know about the company knows all the company secrets and no one else in the company is allowed to know these secrets then we have the salesperson here's the guy who takes care of different company secrets and shows the ones that is allowed to be showed to the person who comes in to buy something inside the company now it's important to note here as soon as this guide the salesperson gets company secrets and shows it to the customer he forgets about the company secret he knows nothing about it and all the knowledge about the secret goes back to the CEO and then he had the third guy he's the guy when a customer comes into the store not to buy something but to sell something he's the guy who takes care of actually purchasing stuff from the customer and giving it back to the CEO so if you have these three people working inside the company and they know nothing about each other again depending on what kind of design pattern we're talking about here they don't know anything about each other so with this story told what I wanted you to sort of Katz it is that we have these three different files that does separate things we have the CEO we said which is the model we have the guy that purchases from the customers who is the controller and then we have the guy who actually sells to the customer who is the view so we have three different people or three different files or classes don't worry we'll get to talk about classes later who has eats their tasks when it comes to the MVC model now there is one more thing I want to talk about when it comes to the MVC model which is something that actually cost quite the civil war down in my comment section of the last video where I talked about the MVC model because there was two groups of people who had learned the MVC model in two different ways and it's very important for me to get this clear so there's no questions about it when you watch this video after listening to the story I just told you might be asking well then we see model can be done in a different way and you are right because there's two different ways you can actually do the MVC model remember this is more or less just a design pattern so it's not that tight when it comes to a specific way of doing things but you should probably stay within the two different patterns and I'm about to mention in order to do the MVC model the same way as other people do it do remember the MU C model or the design pattern is here to make it easier to work with other people so if you do the design pattern in a different way then most people do it it becomes a little bit tough to work with other people so when it comes to these two different ways you can do the MVC model or the design pattern in both examples the model is the one that takes care of the data base meaning that anytime you need to query anything from the data base whether being selecting updating inserting into the data base the model is the one that takes care of this no other class or file is allowed to see what is going on inside the database it's very important that all the database stuff is being handled by the model what the CEO as I call it but when it comes to the control end of you there's two different ways that people do it one way is where the controller gets the input from the user and then does something to update the database or something by communicating to the model and if something needs to be shown inside the website to the user let's say the user search for a list of names then the controller is the one that gets the data from the model and gives it to the view which then shows it to the user inside the website but there is another way to do it which is the one that I usually like to do when I use the MU C model the other way is where the controller gets the information from the user and updates or inserts into the database whatever it has to do in order to get the data from the user to the database the view then communicates with the model in order to get data that has to be shown inside the pages inside the website and then shows it to the user so the controller doesn't get the data and feeds it to the view instead the view gets the data directly from the model and like I said it's important that people don't start freaking out in the comments because their teachers don't do it in this one way and I decide to do it in another way because they are both valid ways to do it it really depends on how you decide to set up your project and I just want to say it is really a minimal difference it is whether the control is the one that feeds the view or the view just gets the data directly from the model it's really not that important and it doesn't really make any big changes to your project security wise or anything else like that so you know it does really matter which model you choose the one I will be focusing on in this course though is the one weather you gets the data from the model not the controller to feed the view and before you freak out don't worry if you don't understand what I say about the MVC model in this video here we will get to do practical examples where you'll get more hands-on knowledge on what exactly the Model View is and how it works when it comes to actual building websites so eventually we will get to a point where you get to use the MVC model in a bit more practical sense so you can understand it better so with all this said in the next many videos I will be teaching about classes properties and methods and if you can't quite see how they're supposed to be used inside a real website after I've taught it in the next many episodes don't worry too much about it eventually in about 13 episodes or so we'll get more into how to actually use what you're about to learn inside real website so if you can't quite see how it's supposed to go together and how it's supposed to replace procedural PHP don't worry we will actually get to that point just keep watching the videos if it doesn't make sense right now it will eventually at some point it's just gonna make sense to you and it will just sort of click and everything falls into place and you know everything there is to know about opticore into programming so I hope you enjoyed this video and I'll see you in the next one [Music] [Music] [Music]
Info
Channel: Dani Krossing
Views: 139,955
Rating: 4.9728813 out of 5
Keywords: Object Oriented PHP Tutorial, oop php, php oop, php, oop, object oriented php tutorial for beginners, mmtuts php, php tutorial, learn oop php, learn php oop, learn php oop tutorial, php programming, php programming tutorial for beginners, php object oriented programming tutorial for beginners, php mvc, mvc model, mvc model explained, php mvc tutorial, php mvc tutorial for beginners full, php design pattern, model view controller, php mvc design pattern, mmtuts
Id: 3OKOe7CraGY
Channel Id: undefined
Length: 11min 56sec (716 seconds)
Published: Tue May 21 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.