Zum Inhalt springen
L

Das Video kommt von YouTube: erst beim Abspielen verbindet sich die Seite mit YouTube (Google).

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

Dani Krossing11:57 273.270 Aufrufe veröffentlicht Auf YouTube

Das Wichtigste aus dem Video

Tipp auf eine Zeit – das Video springt genau dorthin.

Transkriptautomatisch erstellt · 81 Zeilen
Herunterladen
  1. 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
  2. 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
  3. 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
  4. 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
  5. 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
  6. 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
  7. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. 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
  16. 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
  17. 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
  18. 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
  19. 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
  20. 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
  21. 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
  22. 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
  23. 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
  24. 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
  25. 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
  26. 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
  27. 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
  28. 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
  29. 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
  30. 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
  31. 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
  32. 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
  33. 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
  34. 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
  35. 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
  36. 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
  37. 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
  38. 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
  39. 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
  40. 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
  41. 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
  42. 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
  43. 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
  44. 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
  45. 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
  46. 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
  47. 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
  48. 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
  49. 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
  50. 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
  51. 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
  52. 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
  53. 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
  54. 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
  55. 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
  56. 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
  57. 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
  58. 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
  59. 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
  60. 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
  61. 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
  62. 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
  63. 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
  64. 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
  65. 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
  66. 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
  67. 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
  68. 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
  69. 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
  70. 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
  71. 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
  72. 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
  73. 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
  74. 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
  75. 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
  76. 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
  77. 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
  78. 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
  79. 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
  80. hope you enjoyed this video and I'll see you in the next one [Music]
  81. [Music] [Music]

Zum Nachlesen