Transcripts
1. What Is Object-Oriented Programming: Hey there, John Morse. Here. John Morse online dot com. This lesson. We're going to kind of give you a little introduction to object oriented programming. Talk about what object oriented programming is, why you might want to use it and really, hopefully just kind of give you a base of concepts and knowledge and understanding that we can then build on as we then step in to the code. So that said, here's exactly what we're gonna be talking about. We're going to, ah, define object oriented programming. We're going to talk about some of the reserved keywords class property method and then also talk about difference between classes and objects. I'm gonna give you some analogy. He's hopefully, hopefully a little bit more informative than the ones that I dealt with when I was learning all of this stuff. Hopefully help us make it a little more clear, and then we'll talk about why you would want to use object oriented programming and why it's become kind of such a big thing and generally recommended in most situations. All right, so that's a D agenda. Let's dive into this then. So let's talk about the definition, so I'm gonna kind of go through this and point out things that I think are important. So object oriented programming is a programming language model. Now I want to focus on this word model. You could also use the word I think, style or approach or paradigm, whatever word works there for you. But the thing to understand about object during programming is it's not like no some different set of technology or different language or anything like that. It's just a different approach or style or model of the way you go about programming. And it's organized, as the definition here says around objects rather than actions and around data rather than logic so well, we'll just continue here. Historically, a program has been viewed as a logical procedure that takes input, data, processes, processes it and produces output. So a good example of this is if you were to take, say, a just a regular kind of PHP form, which, really, in a lot of cases is the building block of any application if you really think about it. But just take a regular regular kind of HTML form that you would submit through PHP in and store in a my sequel database. Generally, the way that that's been done or the way that people would think about it is you would start with kind of the user interface, which is the form you would take the data that's there and your actual scripting language of your programming language. PHP would would think about it in terms of what I need to do to process it. It would think about it as more of an action and the logic needed to fulfill that action. So it's very self contained in a sense that it's only concerned with that form what needs to be done to process it and ultimately store that out in the database. And then, of course, if you're gonna retrieve that data, then you have a whole nother set of acts, another action and the logic necessary to perform that action. So procedural code is generally focused around this idea of actions submitting this form, storing this data here, retrieving this data, displaying it here, etcetera, and that's how things had been done for a long time. The problem with that is that when you start to build more advanced applications, if you are only focusing on actions and you're not really concerning yourself with the rest of the application. When you're building another part of it, you start to re use a lot of code. You start to have things that can conflict with each other, and you end up with this really big kind of potentially disorganized Ah, just kind of library of code that doesn't necessarily have any sort of organization to it. And when you do start to organize it, what you'll find yourself doing is organizing in in a way that follows, along with Cooper principles or object oriented principles. And so really object oriented programming is taking that idea of Hey, how could I organize this a little bit better and really just kind of extrapolating it out and PHP now supporting it with with some of the functionality that that has come out with it and so forth toe where you can do that? So as the definition continues, the programming challenge was seen as how to write the logic, not how to define the data. Object oriented programming takes the view that we really care what we really care about, of the objects we want to manipulate rather than the logic required to manipulate them. And that's a really important point, because when you think about an application, you start to break it down. Ultimately, what you do care about are the objects. So, for example, if you're working with the CIA, CMS, you don't necessarily care exactly the logic that's necessary to for a user to create a post right, however you got to do that is somewhat trivial to the fact that what matters to the program is that the user one object can create. That's basically a method or an action. Another object the post. Okay, so you have two objects, the user in the post and you have one action which is create or edit or delete or categorize etcetera. And so when you start to look at it that way, what you realize is that an application is really just a collection of objects and actions , its users creating post users, deleting post users, categorizing post users, creating categories, users creating tags, etcetera, etcetera, etcetera. So when you start to think about it that way, you're actually thinking about it in the way that the application itself works and ultimately, the way the end user is going to relate to it right? Then user doesn't care how the Post is created in the back end. All they care is that the Post gets created. And so it allows us to write it in a way that makes more is more in line with how the program actually works and as a result, also be a lot more organized, not repeat, a bunch of code and all sorts of different advantages that we're gonna get into, right? So hopefully that gives you some a little bit clearer understanding of you know, what object oriented programming is in some of the reason why, initially, why we would want to use it. So next let's talk about some object oriented programming basics, and I'm kind of confining this primarily to PHP just because that's what this course is. So, uh, the first thing is what we would call the class. Now a lot of people think that the classes, actually the thing that you're creating and as the coder that is really that is the part that your that you are actually writing. But it's not the actual thing itself won't talk about that. What that means here in just a second. But the way to think about the classes is actually the blueprint for creating the project. So when you're writing a class you're not creating an object. You're creating the brute blueprint for an for an object to be created at a later point. Okay, so just remember, the class is really the blueprint. And then inside of a class, you're gonna have the two main things. You're gonna have our properties and methods. So properties are variable that belongs to an object. What that really means generally is it's kind of it's something that describes the object. So, for example, if you take a post, it would be the title of the Post. It's the data about the particular project there. The particular object, a method is actions that either the object can take or that can be taken on the object. So, for example, you have a user while a user can create a post. So it's the user taking an action in creating a post. But it's also the post as an object itself being created, So it's one object taking an action. It's also another option object having an action taken on it. Okay, so but the thing to keep in mind is properties are more like just characteristics. Their data that describe the object methods are action related things that can do or can be done to it right. The object, then, is the actual thing. We'll come back to that in a second and then in Stan Shih ation or two in Stan she ate is the action of creating an object from a class. So again, the class is the blueprint. The object is the actual thing that you create from the class. Okay, so now let's jump into some analogies because I know that when you initially here that that can probably be a little bit confusing. But let's let's go ahead and look at some analogy. So the first analogy is the house analogy. And so the way to think about this is the house itself. When the houses all built the rial thing that exists that has been construction constructed with wood and shingles and citing all of that, the action for you to just be in the real world, forget coating for a second. If you're just in the real world, looking at the house. That house is the object itself. Okay, so when we're doing object during programming What? We're actually creating our rial things. The actual object. Okay, now, the blueprints for the house are the class. So when you're writing the class, you're really just writing blueprints. Okay, Now, inside of those blueprints, you would you And this is where it can get a little little tricky, but it's abstracted away layer. So you would talk about things in the blueprints, like color, maybe shape the number of windows, all those sorts of things. All the properties, air characteristics of the house that make it that house unique from other houses out there . But you wouldn't necessarily say what color or what shape or what number of windows, at least in when we think about it. In terms of object oriented programming, all you're saying is that it can have a color. It could be any color, but it can have a color or a shape or a certain number of windows again. You're not deaf necessarily defining how what those specific details actually are. The methods, then, are the actions that either the house can take or can be taken on the house. So building the house, heating the house, selling the house, taking a sledgehammer to the side of house, whatever any sort of action related to this particular object or the methods, and then the actual construction is the instance she ation. So in Stan Shih ating ah, class and creating an object is the actual construction of the object from the blueprints. So you're building the house from the blueprints that you created in the class and in the class. You have different properties and methods that that house can have. And it's in in Stan Shih ation where you would where you would tell, or you would designate what the color is, what the shape is, what the number of windows are. No, it's a little that's a little fudged, because you don't have to always declare all that stuff and say a constructor method. When you create the class, you can define those properties explicitly beforehand. You can do that, but so understand. You can have ones that are defined explicitly beforehand, or you can have ones that you define when you and Stan she ate it, but colors a really good example, right if you you had a set of blueprints to create a house, those blueprints wouldn't necessarily necessarily tell you what color the sighting on the house is gonna be. They would just say it's gonna have citing. And you would go to the homeowner. You'd say, Hey, what color citing do you want or what sort of countertops you don't want granted? You want courts or you want something else, right? The homeowner would be able to pick all that stuff out, and it wouldn't really affect your construction. So you don't have to define those things specifically, uh, in the blueprints. But what you're defining is that there is citing, and that there is going to be some sort of of tabletops. So when you actually construct it, that's when you would determine those things is the same same idea here there are certain properties that you don't have to define, You know, the color of the house you can in the actual class. You can define that when you and Stan she ate it. Okay, so, again, the house is the object. The blueprints are the class you in stance eight that class to create the object and Of course, that object will have certain properties and methods associated with it. All right, let's take another look at the car analogy. So again, if we're talking about a car, the car itself is the object is the actual will re a world thing. The three d model of the car that might be in a computer that is being built from that would be the class against the blueprint or the model. You know, the color of the car not necessarily the specific color, but that it has a color. The engine size, the wheels except for those are all properties. Their data about the car methods are things that the car can do or can be done to the car. So start the car, drive the car, turn the car and the actual manufacture or building of the car Is thean stance. See ation. Okay, so again, I just want to really try to make the difference is clear. The object is the actual thing that's created. The class is the blueprints for creating it, and the way that you create it is by instance ating. An instance of that object from the class from the blueprint that's what instance Asian is and again with the nice thing about object. Dorian programming is because you're not saying specifically what color something has to be . As an example, you could create a blue car or a red car or a green car, or you could, in one instance, when you instance, eat the class. You could say this particular instance of the classes going to be of this object or whatever is gonna be blue. Here, it's gonna be green. Here, it's gonna be read, etcetera, etcetera, so you could have multiple instances of the same class that have different properties associated with. Um, as a matter of fact, if you take a look again at a CMS all of the individual posts that you have on a blawg, for example, those are different objects, those air, different instances of the class. The class doesn't tell you what the title of the post or what the content of the post has to be. It just tells you that there is a title, that there is a description. There is content. There is a tag category, etcetera, so each row in your database of your for post is really and different instance of that particular class. Or it's a different object that you've stored in your database. Okay, so again, I know some of you that alone makes sense. Some of you that would be like, OK, you know, I'm still a little bit confused. Or maybe I'm more confused now. The thing about this is that stuff's in your brain now. So when we get into actual code and start using it, some of this stuff hopefully we'll start to click for you a little bit more. And if not, come back and watch this and just kind of bounce back a little bit between the two if you need to. Ah, in order to really clarify it for you because really, we're just trying to lay down a foundation here. It's not necessarily necessary, or even necessarily true that you're gonna just completely understand it all here. You really have to start working with it in order for that to happen. All right, so let's talk about some of the reasons why you would want to use object oriented programming. We'll probably the biggest, most important one or the one that's talked about a lot is what's called encapsulation. And what that means is your bundling data with the methods that operate on that data. So we look quite literally just covered exactly what that is. The properties air, really, the data and the the methods are the methods that operate on that data. So what's happening is you're putting those two things together in a single class, and what that allows you to do is build individual classes that do very individual specific things so you can create, say, a post class. And that class will have all of the data that's necessary for you to build an object and store that all of the data necessary for that in the database. It will have all those properties defined, along with the methods that are going to allow you to interact with that post in whatever ways air necessary to create it, add it to delete etcetera. Then that will be one class. Then you might have another one. That's a category that has all the data and methods. For that I have another one that's tagged for all the methods and data. For that, you might have another one. That user for all of the methods and data for that. So you're thinking about the application in terms of the actual objects and things that are inside of the application posts, categories, tags, etcetera, and you're organizing them by those things and keeping them very encapsulated so that Ah, you're not. You're not mixing and matching coat all over the place. And that leads to ages being a lot more organized and easier to work with, so it makes it easier to scale. Ah, kind of the dry principle, which is noted here under inherent SA's well. But it also happens here you're not gonna necessarily be reusing a bunch of code and so forth. So encapsulation is the big thing leads to just a cleaner, more organized, more efficient, more scalable type. Application. Inheritance is another one, so inheritance is essentially, it's the It's a process that allows objects to acquire the properties of objects of another class, so essentially you can extend on object, and when you extend, you can extend a class. When you extend that class. Basically, the new class that you're creating inherent all of the properties of the class that it's extending and can have access to those methods and so forth. And so it keeps you from having to if you want to add on to or do you work with or you want to use a particular class, but you want to add things to it, you don't have to completely rewrite it. You can simply extend it and then add to it what you want to add to it. It keeps you from again having to to rewrite a bunch of code, don't repeat yourself and it improves usability and all that sort of thing. So inheritance is another reason why the last one is what's called Polymorphism. So different instances can take different forms. This is basically what we've kind of talked about already, which is one instance, you know, the car can be read another complete blue, another can be green and nothing changes about the class. You don't change the class at all. All you do is change the information or the data that you pass to the class when you instance seated, or when you're working with the methods or based on certain conditions and so forth, you can change the object you get from the blueprint without having to change the blueprint itself. So again it creates a level flexibility for your application. It also makes it again. So you're not repeating your code. So you're more reusable again, more scalable, all those sorts of things. And so this is why you see object oriented programming becoming this really hot topic really highly recommended and in most cases is something that you would want to use. Now there are some instances, and this is really is probably controversy, I could say is highly controversial. But I would say there are some instances where object oriented program isn't necessarily the best way to go about things right. So the good example that I like to use his WordPress themes now parts of WordPress themes absolutely should be classes. There are individual things that you would want to do in a theme. For example, Paging nation might be in a good example where there's a number of things that you needed to do in order to page in eight properly, and all of those things are related to each other so you can group all of those things together, and you can use that class to essentially handle that part of Of of things right. So you would have you would still have classes inside of your WordPress theme. But the actual when you created the WordPress theme, you create a functions file, right? The act that actual will functions file where which is essentially kind of becomes your controller. If you're thinking about this in terms of ah M V C mall of you can controller type set up that functions thought kind of becomes your controller a little bit. It's debatable whether building a class in the actual controller and using it there. I think it's debatable whether that actually helps you or for just creates unnecessary overhead. And so again there are. Or maybe you're you're building, you know, a simple you literally or building one form on your website, right? And it's not gonna become It's not this huge application you're going to build. Or if it did, then you would understand. Okay, Now I should probably switch over and start ah, using ah, taking the object oriented approach and that sort of thing. But it's literally one form you're gonna process well, is it really necessary to build an entire class for that whole thing? You certainly could And if you you're like a stickler for that kind of thing, then by all means. But it's not really necessary, necessarily any advantage to doing it that way. Okay, so there are instances where object oriented programming isn't necessarily any sort of advantage and maybe even adds some s necessary overhead. But if you're building an application, if you're sitting down, I'm gonna build a CMS. I'm gonna build a social network. I'm gonna build ah e commerce a any sort of application where you're gonna have a number of different moving parts. You're gonna have a number of different objects and that are interacting with each other and so forth. Object oriented program is absolutely the way to go. It's gonna make your life a heck of a lot easier. Ah, it's gonna make things more organized, more scalable, more reusable, all that sort of thing. So you absolutely should use it in those particular instances. All right, So, like I said, no, that will kind of do it for for this lesson. But like I said, it's not really necessary that all of that made 100% complete sense. It's now in your head. You've heard it. And as we get into the code, then you should start to Seymour clear examples of the things that we've talked about, and hopefully it starts to come together for you. So again, I'll do it for this lesson. Thanks for watching. We'll talk to next time.
2. Class, Property, Object, Method: either. John Morse. Here, John Morse online dot com. This lesson. We're gonna go through the basic class syntax. So I'm gonna show you how to create a class how to create a property within the class, how to create a method. Had two instance Ian an instance of that class and then how to call a method from our newly created objects. So this is going to give you the full gamut of kind of the concepts that we talked about and show you how to actually use them here in PHP. So you can kind of start to toe toe build upon this sort of framework. So this is a framework that we're gonna and build upon throughout the rest of class, so creating a class is actually really pretty simple. You just need the class keyword. So that is class. And then you need to name your class. So let's do something like my class. Now you can name your class anything. Um, but the sort of standard for how you name it is to use camel case. So capital M capital C. That's not required. It's not gonna throw some sort of air or anything like that that just is tends to be the convention among PHP developers, of course. Do what you want, always. But, uh, if you're looking for what the sort of standard is, that's what it is. So once we have the class keyword that we have the name of our class and all we need is opening curly bracket and then a clothes, of course, a closing curly bracket. And that is essentially our class. Everything else that we're gonna do outside of in stance, eating and is gonna happen inside of here. So when we talk about creating a blueprint, this is where you're creating your blueprint inside off, and it's gonna be what it's going to be is collection of properties and methods. So the way to maybe relate this to procedural code is in procedural code. A lot of times you might create a conflict file, and in that conflict file, you have Constance and then throughout the rest of your code, your function or you might create global variables. You might create variables in the global, uh, name space, meaning outside of a function designation, and then inside functions you might globalize, use the global keyword and then used those variables that were created outside the function inside the function. Well, this is a similar type idea in the sense that properties air kind of like, Ah, there's kind of like Constance for your class. You create a property at the top outside of your functions, or they're like they're more like global variables. You create, Ah, variable outside of your function calls at the top of your class, and then you can use those variables inside of your functions without having to globalize them. So in a way, a class is really just kind of a name space. It's a way of something that you can wrap all of your functions in your properties inside of, and they really only sort of work inside of that particular name space, and you could use it. You could use the same variable name outside of the class, and the two wouldn't conflict. Now there's some caveats and exceptions to that stuff, and we'll go through that. But just in a general sense, that's kind of what this is now again, we talked about we'll object oriented programming actually is in the idea behind it, and the fact that you could write and put all your stuff inside of class and still not really be doing object during a programming will again. We'll dive into that some more as we go along. But I want to give you this base, these based sort of concepts and syntax. So once we have created our class, now we can create our variables and our methods. So to create a variable is pretty straightforward. You just do. You use the ah, use this. Identify her here. Now we'll talk about in a little bit. These were public, private, protected. We'll talk about thes the and how that affects scope and usage and so forth. But ah, for now, we'll just stick with public, and then you just use this syntax of r equals I like. It's just like creating a variable, in a sense, so it's pretty straightforward and that that is a class property. So again, nothing too crazy about that now, to create a method again, we use the public key word here, and then you create your function. And then, of course, you're going to do your function name so we'll just do my function and we'll do our opening curly brackets. So again, this is just like a regular function designation that you would right. We just add this keyword public to the beginning of it. And don't really worry about that too much, cause it's I'll explain what it means. But it's not that complicated or big of a thing. Okay, there's some just some difference in areas where you might use one or the other, but it's not anything that's overly complex and would be difficult to understand. So great the name of our function. And then inside of this, we're going to do something so here will just do echo and will use the this keyword and then we're going to, uh, echo var. Okay, so let me explain this. Whenever you are inside your class like this, you can use this this keyword right here. Or you have to use this this variable that when when you create a class, this this basically becomes available and you can use it to call either properties within your class or functions so we could do it wouldn't make sense inside of here, But we could do my function like that to call our function. Okay, so the way you reference properties and methods inside a class is using this using this this variable and then ah, this pointer and then the name of the variable or the property or the function or the method so and just in case is not clear. Inside of a class, variables or cold properties and, uh, functions were called methods. So if I interchange those two, forgive me a little bit on that. But that's the That's the basic idea. So again you can reference those using this this ah, variable here. And so that's and and that's what we do here in order to reference, are variable appear now. The one caveat to this is we'll talk about class Constance and so forth and static functions and so forth here in a little bit and few lessons ahead. But this only works when this gets instance, seated as an instance of an object. Now, in most cases, that's not going to matter, because that's what you're gonna do anyway. But if you are creating a static function, you can't use the this inside of it because no object was instance seated when you use a ah static function again we'll get into that more in a little bit. But that's just gonna start. You thinking about that a little bit now. So again, we're wrecked. So what this function should actually do? If we call this function, it should echo out. I like Coop. That's ultimately what this should do. So now, two instance eat a new instance of this. What we're gonna do is we're going to do my class equals new my class. So this is in stance, eating an instance of our class. Remember the classes, the blueprint. This is the actual thing that we're creating. This is the object. So my class, this variable is an object. This is you know, if this is the blueprint for the house, this is the house. So now we have our house inside of our variable my class, and we can call, weaken, reference any of the properties, any of the methods, and build whatever's available inside of our blueprint. Our class. We can build that using this new object. Okay, so now what we can do is we can do echo. Well, actually, don't even do that. We can do my class and my function and simply call that like that. And when we come over to our page here, you could see we get I like Coop. Okay, so that's the whole idea here. And what this again, What those allows us to do? Just creating a class is not I mean, technically it is, but it's not really in the spirit of necessarily in the spirit of object during a programming. OK, but what it does is it gives us the basis the foundation upon which we can now start engaging in object oriented programming. And we can create individual ah, individual classes to to ah, handle individual functionality. And that's really the whole point of this. Is that for any sort of thing that you want to do in your application, that is, you know, more than just maybe one function, you want to start to think about putting it inside of a class and having that class handle everything. So, for example, if you look at the CIA CMS, you will have in a CMS, you'll need to create edit update, delete posts, right, well, that you should have one class that essentially handles all of that. It's your post sort of class and it does everything that needs to be done for that particular object. So it's going to allow you to create it. It's going to do all the interaction with the database that needs to happen with your database class. That needs to happen in order to insert it into the database. It's gonna do all the interaction that you need to pull stuff from the database upstate, update stuff in the database, delete stuff in the database and anything else that you might want to do that you think belongs in that. So now you have that all self contained in one class and all you have to do instead of rewriting that code every time, every time you want to get a post from from the database, instead of having to rewrite all the database clothed that's necessary to do that, you've created instead a function or a method inside of your class that does all of that, and maybe all you need to do in order. Let's just say this word that you pass in the post I d. And so let's say this is, ah, get function. It's a reading function. You pass in the post I d. And it does all of the connection connecting to the database, getting that I d. From the database. No pulling that data, putting it into something that you can work with, whether an object or an Iranian PHP and then returning that to you. So now here you could be like post equals my function. And now, on post, you would have access to, let's say, no echo post title echo post content, etcetera. You would have access to all of the data inside of that. And then maybe there's helper functions that you have inside of that class that allow you to, I don't know. Maybe you want to echo post, and you're gonna do format and, ah, post date or something like that. Okay, So and and maybe you would just, you know, maybe you would have to wear when you call Post Date. It's already formatted. You, of course, could do that. But the idea is that you can create methods that are helpers or that will allow you to do something related to that object that you might want to do. So the whole idea again is, is not just to know how to create a class, but how to take it. And then for the objects that that you have in your application and the things that you want, those objects be to be able to do you gender. The general rule of thumb is that you're going to create a class for each one of those that's gonna handle all of the stuff that you need to do now. That again and I want don't want to get too confusing here. But that's that's a secondary layer that that that those classes would sit on top of, ah, more foundational layer, which would be your database class. Have you seen any stuff I've done with database classes? You know those those classes have function. They have basic interactive database interaction function, so you don't have to write all the queries. You can just call methods from inside the database class so your post class wouldn't be querying the database directly. It would be calling methods from your database class, and so that's how you that's how you layer an application. If you look at say, for example, WordPress, they have a WordPress database class. The only thing that database class does is interact with the database. So there's a few methods, like get results Get row Queary Update. Delete etcetera that are methods that you call and you don't run the queries. You just pass in data. You pass an information that at that the the methods require the parameters, and it does all of the database stuff for you connecting, running the queries, returning the results handles all of that. So that's a base level class. But on top of that, they have a query class in that query classes how they query for posts and pages and categories and so forth. So the database class is a foundational class that's a base level class that interacts with the database directly. Queary is a class that doesn't ever interact with the database. All it ever interacts with is theory. WordPress database class. So again, that's how you layer. That's how you use classes to layer an application. And that's the thing I want you to start thinking about is how can I start taking all of this code that I have that maybe all sort of intertwined between the queries and the front end views that I'm displaying in the database it's all sort of mix together. How can I start pulling it apart to create individual classes that do individual things Have one class that interacts with the database. Every other class sits on top of that. Any class that wants to interact with my database has to go through my database class. And so then I have, you know, maybe a query class or a post class and then, you know, maybe you have a views class that sits on top of that, an exception. We could talk about some more of that stuff, but I just wanted to get your mind thinking in that direction. So again, being able to do what we've done here is the foundation of all of that. So that is a really, really kind of straightforward, simple way of how to create class, how to create a property, how to create a method in stance, dating an instance of the class and then calling a method from that class. So hopefully got something out of that. We'll obviously continue to build on this as we go through. Ah, the rest of these lessons I'll see in the next videos
3. Constructors: Hey there, John Morrissey, John Morse online dot com This lesson. We're gonna talk about constructors. And so what constructors allow you to do is to have something run automatically when you and Stan seat an instance of an object. So to show you this, let's go ahead and just copy all this code here and let's open up our new file. And so we're going to create a new function called Public Function and under underscore underscore construct. So that's the name. That's how you name the constructor. If you're looking at really old tutorials, you may see tutorials where there's a method in there that has the same name as the class. So it might have been something like, um, function my class. That's how constructors were done in the past. But now there's an actual constructor method that you want to use. I think at this point it even will. If you use the name of the class for a while, they're both would work this constructor method and the function with the same name is the class. I don't think the function with the same name of the class even works anymore, and regardless you you want to use this. You want to use this construct method here. So what this does is anything you put inside this method. When you and Stan stated an incidence of the class, it's going to run this method automatically. You don't have to call it or anything like that. And so this is good for anything that you need to do to set up your database. Me? I don't know. Maybe there's maybe you wanna put if you're writing a database class, you want to put your my sequel connection details in there? Um Or maybe there's something you need to do with Post at our There's lots of things that you'd want to do within a constructor. But the main thing is that it runs when this client is classes and Stan seated. So just sort of show you this. What we'll do is we're gonna take this var, and we're gonna change it to man. Coop, is I okay? Eso what were what we've essentially done here is we have this variable that we've set up here, but now in our constructor were resetting it to something different. And so when we instance eight an instance of this class. What should happen is this will run. And this will overwrite what we've set here. Actually, real quick. I need to remove this post idea because we don't actually have one. And so now when we come down here to echo this, it should echo what we've set in our constructor here. So let's go ahead and load this page and you can see we get mad. Hoopers, I So that's what constructors allow us to do. And now it doesn't have to be just overriding variables that could quite literally be anything that you want to do here that you need to set up. That needs to run right when this isn't stance sated that the rest of the class is gonna be pretty much dependent on now. Another thing that you can do with constructors and important to know is you can put in variables, so let's go ahead and just change out a variable here called text. Now, instead of hard coding this here, we can get rid of that. We could set this equal to text. So now what's gonna happen is we can pass in the text into our class When we instance eat it and I'll show you do that in a second. We can pass this in and now are variable. Ah, of this. This variable here will be set to whatever we pass in. And so what you have is a fairly standard sort of thing that you'll see in classes in that in the variable decoration appear, you'll have some sort of default. This now is basically serving as a default. And here No, you will have something that you can override it with in your constructor here. Okay, So if we come down here now, in order to use this text now we have to add in our parentheses and then we can pass in our text right here. So if we save that and refresh that we're going to still get May Lupas I and then we can change this to anything. We want to just say hi and you can see now that's getting past in. Now, if you start to think about that a little bit, you can start to see the power of some of this because now you can. Whatever this text is, there's you could go through and maybe you want to form at this text. Maybe you want toe. I don't know. There's also maybe on a, uh, change what the text is based off of certain conditions or there's all sorts of different routines. You could run this text that got passed in through, but you could literally pass in any text. And so this is the whole idea of what a class really is when we talk about Oh, in conceptual terms, that the class is a blueprint and the object is the actual thing. And it is the data that you have when you instance eat an instance of a class that changes what the what you get from the blueprint. So meaning, you know, a blueprint for a house doesn't tell you the color of the house. So when you actually go and create the house, that's when you decide on the color. This is the same idea. This doesn't tell you what the text has to be, right? It doesn't say, Okay, we may have a sort of default, but it doesn't really say what the text is. You can pass in any text. So now this object. Okay, if we do this again on will go. Just go new class and will reference new class here. We can get rid of this post equal stuff. We don't actually need that here. Okay, so I want to show you this. So we have two different instances of our class. The blueprint is the same. But all we've done is created two different instances and we've passed in different data. So we have my class, and then we run my function. We have new class which passes in. Hey, so if we refresh this you see, we have high and we have Hey, those air literally two different objects. Those air, two different houses. And the way that we change them is based off of the data that we passed into our our class here. So that's the power of this. We change. Nothing appeared to get these two instances. Now, of course, as you have more complex applications, you're gonna have mawr complex classes. They're going to doom or complex things. And so the differences here, and maybe the data you'll pass in here or the data you're graph from a database or whatever . Whatever it happens to be doing, it's gonna make those two objects very, very different. If you think about it, just take WordPress. If you think about two different WordPress posts, will they have a completely different title? Have completely different content? They're going have completely different featured image date that they were created, maybe author. There really two completely different to post on a blogger, two completely different but the class toe blueprint for getting them to the point where they're displayed on the page and storing in the database and everything that needs to happen. The class is the same, so you can create two very different objects from the same blueprint. And that's really what object oriented programming is and again getting back to individual objects sort of having individual classes that that deal and handle with handle there. They're functionally, it's not a hard fat, and I don't want to mislead and then say that that's a hard, fast rule. You absolutely have to do that, but that is the general sort of way that you kind of look at it. Ah, at least at a baseline how you look at object during in programming so again that's dealing with constructors. That's what they allow you to add to your classes that make them that can start to make them really, really powerful. So hope you got some out of that. Thanks for watching We'll talk to in the next one.
4. Visibility: Hey there, John Morris. Here, John Morris, online dot com. This lesson we're gonna get into visibility and visibility deals specifically with ease, designators these keywords right here. No street that you can have. There's public, there's protected and there's private. So I'm gonna copy this code right here. We're gonna go into our new file, and I'm gonna create something real quick that will help, I think, make this little more clear. So I'm gonna create class. New class extends my class. All right, So what this is doing is this is creating a child class. So this class right here, the one doing the extending is considered the child, and the one being extended is considered the parent. And the reason that is is because when you do this, what happens is you get access inside of new class. You get access to all of the properties and methods that are available inside of the original parent class if they're not private, and so that that deals with are are sort of ah designators here. So I want to show you this kind of from the top to bottom. So when we come down here and we have stance a new instance of a class and we call our function or are method my function The reason that we are able to do that And if we reload this page, you can see we get high. The reason we do that is because this this method is set to public. If I come in here and set this to anything other than public so let's set it to private for for starters. If I said that to private, I get an air. If I said it to protected and then we reload, I get in air. So this is the first sort of designation and the way to explain this. I think visually this will help. But private methods and properties are available on Lee here, Onley within the funk or the class and that created them so private. Are Onley available here protected are going to be available within the class that created them and any child classes okay and then public are going to be available in the class that created him any child classes and outside the class. So in order to access and use it a method or property out here, it has to be set toe public. Okay, so let's let's go through some examples. So let's go ahead and set this back to public. And let's go ahead and set this to private. Now your instinct might be Oh, no, we're using private here and we're calling it here. So and then we're calling that out here, so that's not gonna work. And they're actually that does work. And the reason why is because what we're calling out here is on Lee the my function method . It's the my function method that is now referencing this variable. And these two things are within the same class, so my function can use this private variable. Okay, so that's why this works. We wouldn't be able to if we tried to come down here and go var like this, we wouldn't be able to access of lard directly because of our is a private property. So it says we cannot access private property. All right, so that's a good thing to keep in mind. And then another example dealing with the child classes. If we come down here and we create a creating new functional public function will call it display and inside of it. We are going to simply echo Ah, this and var. Okay, so we're going to echo this var. Remember, this is our private. This is a private property, so it should not be available down here. So if we change this to new class and we changed this to ah, display and let's just get rid of this relax, it will leave that for a second. So now what we're trying to do is inside of display. We're trying to We're trying to echo out this private property. If we try and do that, you see, it's blank, we get nothing. And that's because we don't have access to this. Now, if I come up here and I change this to protected So now remember, protected is available to the original class and any child classes, and I rerun this. Now I have access to it. Okay. So, again, that's the difference between private protected and public. Now, the instances where you may want to do this a lot of times, you may have a method that you use inside of a class that you Onley intend to be used inside of that class. Good example. I can think of is, let's say you're creating a post class and let's say when you pull the post data from the database before you display it, you want toe formatted in some way. So WordPress, for example, does this has, ah, this this ah function or this method called WP auto Top our auto WP auto P or something like that? Basically, what it does is it takes what it gets from the rich text editor, which happens to add line breaks to instead of paragraph tags. So when you're writing in WordPress and you create a new paragraph when that gets saved in the database, it doesn't get saved in paragraph tags that gets saved with break tags. So what WP Auto P does is it when it grabs that from the database and it changes those on? It's a sort of complicated formatting method, but it changes those break tags and figures out what where the paragraphs they're supposed to be in actually use paragraph tags. So it's just it's a formatting thing. Well, that formatting method may not be something that you want available to to everything else and just to kind of put a fine point on it. If you have a formatting method for, say, example, your post class, you really don't want to have that than also available to, say your category class. Okay, so that really you would want that to be a private method because you don't want to be kind of using having a category class that's using a method from your post classic two completely different objects. That's not really a good thing to do this on a best practice. If you wanted to create a formatting method that you then used in, say, category and post on DSO fourth. Where you would do is take that formatting meth formatting method out of the post class and you would create a format class, and then you would any time you wanted to format something, you would call that format class, and you could call it from category. Or you could call it from post. You could call it from wherever but you would. You would encapsulate it into its own thing if you wanted to use it in multiple in multiple classes, you don't want to be using a method from, you know one class in another class. Generally speaking, you want to keep them encapsulated to be their own things. And so one of the ways that you can do that is by making them private methods. If it's a private method or a private property, other classes can access. Or if it's ah, protected. Other classes can access it unless they extend. Ah, the one Ah, they extend the original class. So again, that's the difference between public. Peyron protected that some of the background information in context about when you would want to use these Ah, and so forth. So hopefully have found that helpful. Thanks for watching we'll tucked in the next one.
5. Inheritance: Hey there. John Morrissey, John Morse online dot com This lesson. We're gonna talk about inheritance, since we kind of started into a little bit. Last lesson would go ahead and kind of go all in on this lesson. So let's go ahead and copy. This code that we had from less than four will come over here to this lesson. And so this extending a class is actually pretty simple. And what it allows you to do is inherit all of the functionality that is not private from the original class. So when we talk about thes and we have new class extending my class new classes, what we would call the child class and my classes what we would call the parent class since new class extends my class, my class is the parent and new classes. The child gets inheriting new classes, inheriting from my class. Okay, So, again, what this allows us to do is anything that's protected or public than we are able to use within this new class. Now, the way we can see that is if we go to and we change this to new class Now keep in mind, we don't have any functionality inside a new class right here. All we're doing is extending my class. But we're still calling this my function method here. So let's go ahead and load this. And you see, we get this high right here. So even though we haven't added anything inside of new class, we've inherited all of the functionality from our original parent class. Now, just to show you, if we change this to protected and we reload this, then you can see that we get this call to this protected method. Okay, so we are calling this outside of the class here, and this function is protected. So that's why we don't have access to this. And then if we do, you know, private like this, of course, we're going to get that as well. Now, something that we could do is if we change this back to protected. And let's say we add a new method inside of here. So do public function, and we'll do display and inside display. All we do is we call this and we call my function and then out here going put our semicolon . Oh, here a new class we call display the new method that we created our new class. So let's go ahead and run that, you see, we get high again. Now the reason that we get high is because we are from we are calling my function now from within a class that extends it. So even though it's protected, we still are calling it from within a class that extends it. So this is allowed. And now we're calling the public function display. That's what we're calling out here. So what we would have to do is, if we set this to private, we set that to Private. Ah, Now we're going to get the fatal air call to private Method. Okay, so we're not able to do that. So that's how the inheritance works in all this. Now again, you know the way that you want Teoh, use this examples of how you might use this. I mean, let's say so. One example I can think of is, let's say you get ah, on SdK from some site, like maybe YouTube for interacting with It's a P I, and you want to build a wrapper around that Well, one of the ways that you could do that would be to simply extend whatever class or classes that are usually in these SD case, there's gonna be, like, a standard sort of based class that you so you could extend that class. Um, in order to use its functionality, bring all of its available functionality to your new class that you're gonna right, that is a rapper, that because you're going to use you to have been some specific way so you could take something that's out there, that somebody is written there that is existing. Or maybe you've written and you could extend it and then add in your own very specific functionality or you have, say, a database class. And let's say we talked about a query class in the database class. Maybe the way you want to use your database class is to just simply extend it with your query class. Now you want to be a little bit careful of that because of the way multiple inheritance works. And it could get a little tricky when you you know, if you want to use your database class and in query and in maybe another class in another class in another class than that can all get a little bit tricky. But ah, you know, if if that's the queries, the only class is going to be interacting directly with the database class, then maybe that would be an option for you could do that. Um, that's not the only way to interact with the database class, but again, it's an option. Or maybe you create an object class that is sort of like a base framework. And then from that you create a very specific post class that adds to what you have an object so that you can, ah, have stuff very specific for post. So again, I'm just thrown at some examples of different ways that you might be able to use this. But again, the big thing is this New class is going to take all of the, uh, properties and all of the methods from its parent class. Ah, as long as those are either public or protected. Now there's a couple things to to keep in mind here. First off, when it comes to constructors, the child class will not automatically call the constructor of the parent class, so you have to use some sort of specific syntax. So we go public and we're gonna go function. And we're gonna create our own constructor inside of our ah, inside of our child class. Now, if we leave this like this, you can see we go back to getting I like Coop. Now why? Why did we go back to that? Because we overrode before we didn't have a constructor in our child class. So we just used the parent class. Now we've actually created constructor insider child class. It overwrites the parent one. So this parent one isn't going to get run. That's why we go back toe. I like Coop because we need that parent constructor to overwrite what was in our variable here. So that's why that happens now. Maybe it might be the case where you want this constructor to run still. So what you do is you use parent colon colon and then construct So you're essentially calling that parent method. So now this is going to cause that parent constructor to run. So if we rerun this Ah Oh, actually, we need to pass in our variables, so let's go text and we need text. All right, so we have variable. So we need to make sure we put those in there. So now if we rerun that, we go back to getting high. So that's something to keep in mind when you're you're working with this. This also opens up something. Ah, that we may not that that may not be immediately obvious, but it shows you how you can do this because we have this constructor class here and we have a constructor method here. You might think. Well, we have two of the same name. This is how you override methods from the parent class in your child class. So, for example, we could create a method called public and we could do my function again. This is the same name is what we have up here now instead of just doing Ah, let me go ahead and my function. Key word. I'm pretty horrible about that. Actually, you might notice that I do that even when I just coat on my own. Um so my function now what we can do is maybe we want to change this. Maybe we want to take eco this var, and then we're gonna add something to it. That's like, um hi. Well, it's just out of period since we did in the original one. My name is John. Okay, so let's go ahead and refresh that. You can see now we get Hi, my name is John. So we have two methods with the same name, but there's no error because what PHP does is when you extend like this, this class will overwrite the class in the parent. Okay, so that's how you can extend a class and have access to the data that's available in the parent or the functionalities available in the parent class. And then if there are certain methods inside of that class that you want to overwrite, you can do that as well. And obviously, if you want to in here you can overwrite properties. I mean, as well you could, you know, this var equals okay, so let's see what that gives us. So now we have something different. My name is John. Why do we have that? Because remember, we passed in high is all we passed in so that that goes to this class high. But then we run that through our parent. Our parent comes up here and says, OK, that this var is now equal to test. Then we right after that changed this var to something different. So this basically gets an, uh, over written by this right here. And now it's this that we're going to use down here when we do this stopped far of our period. My name is John. Okay, so that could be a little bit confusing. But what I'm trying to show you is that you can override virtually everything inside of the parent class. The methods, the properties, all that sort of thing. So you can grab the functionality you want, and then if you want, there's you want to add to what's there, you can add to it by creating new. You can create new methods inside of here like we did with display. You can also override existing methods and existing properties inside of it. Now, the one final caveat to that. Let's say I put this keyword final on here on my function. Go ahead and reload that you see, we say cannot override method my class, my function. So when you're creating your parent class, if you want to create a method that cannot be overridden by child classes, you need to use this final keyword right here, and that will prevent someone from being able to do this. Okay, so that's a really deep dive in on in into inheritance. Hopefully, that's pretty clear for you. If not, I would say just kind of go back through it and then actually get in there and start messing. And she's changing the keywords from private to protected toe, extending a new class over it. Just get in there and mess with it. I heard this great quote the other day. That clarity comes from action, not from thought, so that I think that's very, very true. And that's gonna be very, very true in this case. The more the message that the more you use it, the more it's going to kind of make sense and and and become clear for us. So get in there and start messing with this stuff, all right, that will do it for this lesson. Thanks for watching. We'll talk to you the next one
6. Static Methods and Properties: Hey there, John Morris Here, Joe Morris, online dot com. This lesson. We're going to get into static properties and methods and really talking about the static keyword because that's what you add to both. That actually makes them static. So let's go ahead and copy this code right here and go into our new file hair. And we're gonna quote, kind of sort of simplify this for a second so that I can show you this. I'm gonna get rid of the constructor for a second. I'm gonna change this back to public, and we'll change this toe public and we'll call it will add this word static to it. Okay. And then let's come down here and let's instance a new instance of our class. So we'll go. My class equals new my class. You don't have a constructor now, so we don't need to worry about that. And let's just see what happens if we go my class. You know my function. So we're basically doing what we did before, right? We have a method. My function, we're referencing our property appear and we're out here and we want to just echo that and actually, one more thing that I need to do before we do this, you may or may not need to do this. My system happens to suppress airs. So I need to turn on air reporting and so e all again, you may or may not have to do this because some of the errors were going to get here aren't actual fatal airs. They're gonna be notices. And so I just want you to be able to see those. And if I don't do this, you're not gonna be able to destroy See those someone to display airs, and we're gonna set that Teoh true. All right, so that should be what we need here. So let's go ahead and reload this and just see what happens. So it says strict standards accessing static property. My class of our as non static in bubble online nine. So right here we're referencing are variable like we did before. But now it's giving us this air that we're referencing it as non static. So this is kind of the first sort of introduction to what this actually means When we create A when we create a static variable or a static method like this What it does is it makes it so that this this variable, for example, basically exists outside of the I would say, the object scope of this class meaning if I tried to use this If I try to use it anywhere in my class and used this variable here, it's not gonna work because I've changed it to a static variable. And it's not meant to be used in the object scope, okay? And it's the same thing if I tried to come down here and let's just try and access the property directly. So if I try to access this directly, go ahead and reload and again accessing static property my class as non static. So when you make it static, it makes it so that you can't access it through this object. Since syntax it, it's no longer a part of that that object context. So now what you need to do in order to access it is you need to use the you need to access and what is actually directly. So now we're going to do my my class, and you may have seen this before, and then let's do my function like this, Okay, and actually, we need Teoh. Actually, it's do far and I think it's like this. So if we want toe access, the key were like that. We have to make sure or the property. We have to make sure that we put our, um, make it into an actual variable here. So let's go ahead and reload this and let's go ahead, echo it. So now we get I like hoop. So now we get access to it. So what I just did as I was able to access a class property without instance eating an instance of the class. That's ultimately what static keywords allow you to dio. And we can do the same thing with our method here. So let's change this to Static. Let's actually change this back to regular, since we're gonna be ah, doing this. So let's go ahead and do my class. My function like this I want to show you this. Now you can see fatal air using this when not an object context. So again we're accessing. This is a static function, so we're not in the object context. So this doesn't work this on. Lee gets created when you instance e eight An instance of the class. That's when this becomes available. Since we haven't done that, it's not available. So let's go ahead and do this. Uh huh. Right now, No, we haven't made this static here, so we'll go ahead and refresh that access to undeclared static property. So now we come back up here and we change this to Static, and we reload. And now we get what we want. So I don't want to show you is that when you start using the static keyword, it sort of starts to take over everything that that you dio. So you want to be a little bit careful about how you're using this? Now, the the reality of this is that you might be asking, Well, why would this this be used primarily, this was used. Ah, this was used before we had name spaces and it was used in the context of you weren't creating a This was basically taking procedural code and putting it inside of a class, and you were essentially using the class as a name space. And so this is what I would call pseudo. This was used for pseudo object or hated programming in the sense that you know, you weren't You weren't necessarily creating a post very post class in a category plastic, you were basically just taking You weren't breaking everything apart and actually living up to the spirit of what object during programming is. And I'm not saying I'm not like ragging on these people. I'm not. I'm not saying that this is like some horrible thing or whatever. I'm just saying that was not generally how this sort of thing was used. This was used in a context where you just had more of a collection of methods inside of your class and you had a collection of properties and you were only putting them inside of the class in order to keep them from being in the global name space and muddying up that global name space and having collisions with other variables and from other applications and so forth. So the idea was to put him inside of a class and using the class is sort of a name space, but still be able to access these things directly. And so, if you if we took this out of the class right here, and we just had my function. We could just call my function like that, right? We wouldn't have to instance eight. A new instance of a class and all that sort of thing. So static keywords allow you to do something similar. You just had to add this my class designation. And now you could access your functions directly. You could access. Ah, you know, you're very your properties directly. And if you wanted Teoh reference a property appear just like if if we were doing let's just do this let's go. My var. And that's in the global name space, right? It's not inside of any class inside of any function inside of any name space, any interface, anything it's it's right in directly in PHP. So this is global name space. So now we create a function and we want to echo ups, fix it in a second, my var, and we're gonna call this my funk. All right. Ah, function my funk. All right. So if we wanted to let's go ahead and echo, They're actually this is call my funk, Huh? Okay. All right. So if we wanted to do this you see, we're gonna get undefined. Variable, undefined. Variable my var right here. Why is it undefined? Because this is in the global name space. And this is in the local name space. So what you would have to do here and if you've taken my PHP one on one course, you'll know this, but go global my var. Okay, so now what this says is I want to use the global version of my var inside of this function . Now, you know, we get, we get what we want, we don't get an air, and we could set this to my bar equals global. And so we get, actually get some output so we get global like that. Okay, so this might have been how your code was written before. And you now just they're like, OK, I need to put this inside of a class because having my var in the global name space will anything could be named my var. I could have some other application and there could be collisions, so I'm just gonna take this and put it inside of a class. But I don't I'm not trying to do all of the separating Allenby module. I just want to take these methods and put him inside of a class. Well, that was generally how static was used. Because now you can still sort of do this This a similar sort of set up to this. But now you just have to use instead of using global and so globalizing with global, you just need to make sure you reference it statically, and you use static keyword on everything. So static keywords, worm or used when you were trying to do this and a really more of a non object oriented ah way, and you just wanted to take kind of procedural code and put it into a class. So classes were more used to the name space when people were doing this sort of thing is the point I want to make. I bring that up because we now have an axe, actual name, space keyword to create a name space, and so we don't need to do that anymore. And when we're talking about object oriented programming, generally when you're using static keywords that are outside of the object context, that's not really object during programming. It's not that you can't it's something you can't do that you certainly can. But if you're going to kind of go the object jointed route and break things into objects and encapsulate Ah and polymorphism and all the fancy words out there that people use. If you're gonna go that route, you really don't need or want to be doing this. There's not necessarily a use for static methods or or properties inside of that true object or in context. Now there are probably some caveats to that, but I would say there probably fewer than you might think. And it's usually a situation where, if you're going to do that, either there's actually a better way to do it. That is mawr object oriented or it's a just like an extreme example on your problem might run into that once in your career. Um, I personally have yet to find a situation where I absolutely had to use a static, variable, er static function for something. Usually I could think about it a different way and actually find a better way of accomplishing what I wanted to. So we cover him because that's a part of what you can do here and so forth. But it's again. It's something I wouldn't call it deprecate ID. But it's something that's definitely not used near as much, especially with the introduction of name spaces in the newer versions of PHP. So that said, that is static, the static keyword that static properties and static method. So hopefully get something out of that. Thanks for watching. We'll talk to the next one.
7. Namespaces: Hey there, John Morris. Here, John Morris, online dot com. This lesson. We're going to get into name Spaces, so we kind of reference that a little bit in the previous video. So I want to kind of go all in on on that here so real quick. I want to give you why you might want to use a name space. So the whole idea is looks kind of come down here. You may it be working with when you're putting something together, you may be working with multiple packages. So let's say, for example, you're working with ah, Facebook sdk and a YouTube STK. And so you're gonna bring both of those into your application. You're gonna include them into your files and your start using them. Well, let's say that for some reason, both of them had a database class. Well, if we didn't have name spaces and we just and those packages had that class named just database like this, if they both had that, you would run into an issue where you have two classes trying to declare the same name and that would throw on air, so that would obviously be a problem and so the way people used to deal with that in the past is they would use really descriptive class name, so they might use something like YouTube sdk data base like this, and that's fine. And it works, but that can start to get a little bit cumbersome. If you start to get longer class names. I think Zen engine kind of the poster boy for that sort of thing. If you look at the class names and and such, they start to get pretty long and convoluted. And so one of the things that was created in order to help deal with this was a name space . So instead of having to prefix like that, the first thing, what you could do and this is gonna throw on air because you need to declare name space at the top of the file. But we'll just, for example, sake we'll pretend like this is the top of file. But you would do something like YouTube and then s d k like this. And what that will do is that will create a name space that now you can put all of your, uh, your classes inside of and they won't collide with other classes that happen to be in a different name. Space. This creates a new whole new name, space. Anything below this name, Space Declaration. So if we do database like this, all of it on Lee exists inside of this name space and no longer is in the global name space . So if Facebook came along and said name space uh, Facebook flash sdk And they did class database these two declarations of the class database , even though their same would not collide because this one is inside this name space, and this one is inside this name space. So that's the whole idea. It z kind of like pre fixing, but doing it in a in a more or less convoluted way. Okay, so let's talk it. Let's then kind of go through the nuance of how all this works. So, first off, I did pre write some of this code just because there's a there's a certain amount of it and so sent me watching me type all that out would be unnecessary unnecessarily long. So I created two files. One is lib one dot PHP, and one is lib to dot PHP. You see at the top of both of these and again. Name space needs to be declared at the top of the file. We have app and then we have slash lib one and we have app and then slash slash Lib to what that does is put each one of these files into their own name space. So now my class and I can actually you know, you can Ah! Ah, You could have these have the same class name. These could be most both my class name, but I would just want to show you, um, some of the differences here. So anyway, I gave them different class names and these classes are what we've been working with. There's nothing extra in here anything. So all we did was add this name space to the top. So now this puts these in their own separate name spaces app, slash live. Want an app slash lib too. Now, if we come over here, then the first thing I do is require those two files in. And now we use this key word use in order to ah, reference are Lib one and my class are lived two and your class and you can see here. I'm using hay and here I'm using high and we can see Over here I get hay high as my output . So this this is essentially how you can start to encapsulate your entire application into name spaces. And then So it's separate from every other application, and then you don't have to create really convoluted class names. So the first thing that you do is you put what at the top of your actual class files, you're going to put a name space and define a name space. And then when you want to use those classes, you used the use keyword and you reference app live on and your class apple lived two and your class. Now there's another way that you can do this if we ah, go ahead and comment those out when you're instance eating a new instance of the class, you can also reference it specifically so you could do app and then you could do live one live Juan, and then you could do hap slash lib liken type lib, too. Like this so you can were access. There were references ing it specifically when we instance eat an instance of that class. So we referenced this. You can see that that works. And to show you what happens if we don't do that, go ahead and get rid of those and will not uncommon ent those You're gonna get fatal air class, my class not found. And it's a not found air because it's that when we create the name space, we take it out of the global name space. So the class declaration literally does not exist in the global name space anymore. And so that's why you get this not found air. Okay, So again, that's how to create name spaces and use name spaces and then use them toe again. Encapsulate your APS now, one other thing that you could do that I want to show you that somewhat handy is weaken. We can also use this as key word so we can use use Apple Iban my class as let's just say J and ah, let's use this on as m. Okay, So if we come down here now, these air called aliases. So now we've changed how we can reference thes things. So if we reload this, you can see I'm getting my expected output and I'm calling new J. I never declared a class called J. I never declared a class called em. But what I did do is I said, use this class as Jay used this class through all the name spaces as M. So I created an alias four my classes. And now I can call I can instance eight new J new M and that that works. Okay, so those are aliases that you can use as well. So that's another thing that you may see See out there and you might find handy as well. So again, when it comes to ah, how to actually go about, ah, organizing your applications and so forth, you know, a lot of times I get people who are like, just tell me how to organize it, and the reality of it is is that's where you're you know, that's that's where you're sort of creativity as a developer kind of comes in. Now I'm gonna do an M M V c primer at the end of this course, and I'm gonna assed part of that. We're gonna take a look at some existing packages that are out there. So you can get some idea of how they're organized, but there's all sorts of design patterns that are out there. NBC is not the only one, and at the end of the day, it's your application, so you can kind of organize it, how you want to again. There's there's kind of best practices and conventions and just kind of ways people have done it in the past. But part of what you do is the developers to be creative, solve problems in unique ways, come up with your own sorts of ideas and so forth. And so don't be afraid to experiment with this. Obviously, take a look at other packages if you want some guidance. But hopefully now, knowing this, you'll know what you're looking at. And I think that's the biggest thing. When you go into these other packages and you see some of this stuff and you don't know what it is, it can get really confusing really quick. But hopefully now, knowing how the name spaces work and so forth, you can go into it and go, Oh, I know what that is, and you can start to put together how they're they're putting things. How their ah, organizing and so forth, and that can give you ideas. So that's my goal. 40 anyway. So again, that's name Spaces. Hopefully. Ah, you got something out of that. Thanks for watching. We'll talk to in the next one.
8. Autoload: Hey there. John Morrissey, John Morris online dot com. This lesson. We're gonna get into auto loading now. Auto loading isn't necessarily kind of strictly object during a programming. But as we start to get into talking about building classes and we start to get into using name spaces and so forth Ah, I think it becomes important to talk about auto load. And the reason why is we're focused on these two lines right up here. And you may have I worked with applications enough for code enough to know that after a while you start to deal with a lot of these. And I you know, I do tutorials where I talk about ah, using ah, load file and that sort of one way of dealing with this and that works well, when you're working with something that is more sort of, uh, on a some people are going to scream Blast for me here. But when you're working with something that's more front and heavy So where ah lot of your code if we were to talk about this like, say, M v c wise is a lot of your codas view heavy, and it's not necessarily as much stuff on the back end. Whereas, ah, what we're gonna talk about with auto load tends to work a lot better when you have a lot of back in like you have a bunch of different classes that you're needing to load a bunch of different name spaces and you have a ton of that stuff going on. And so the idea of auto load is to help us kind of get rid of having to used these requires and have, like, 15 of them. If we have that many classes again, this really starts to become, ah, an issue when you have when you're breaking individual objects. Ah, object handling code into classes. And then if you again, we start talking about NBC. You start talking about multiple classes for one object because you have, you know, views and you have the model and you have the controller and all that sort of thing. So you start to get a lot of classes and a lot of files that you need to load. So having to constantly drop in all of those require statements can be cumbersome. So that's what we're going to show how to deal with here? Ah, I created a new file. Will just copy all of this code will drop this in. And of course, as you would kind of expect if I got rid of those requires we're going to get these airs. Now, I want you to pay attention to this air because ah, what What PHP kinda tries to do is ah, when we go when we go to do the auto loading stuff it's based off of this is the class name . Okay, that that's something. I want you to really pay attention to its app slash lib one slash my class. That's the actual name of the class. So when we get into you may have seen ah ah function called SPL auto load register will come down here to register. So SPL Lotto auto load registered takes a callback function. So we'll just say for our sake here we'll go. Auto load. Okay to keep it simple. Um, now this function is gonna be kind of in this Ah ah. Oftentimes in this global name space like this. So ah, uh, it using auto load like this is, um maybe not the best thing to do. You might want to think about a different name here, but for our purposes, I can just show you this year. Um and what I was doing there is I I'm just in this habit of doing this. This is not a cyst on a new function we're creating. What we're doing is this is a PHP function were calling, and we're passing in a name of a function that we want to use is a callback. So SPL auto load register is just going to call this callback function. So we're gonna create a function called Auto Load and a lot of frameworks. You'll see they put this in, it's kind of own separate file and so forth. Ah, and so you know, the way I'm doing here just the top of a file like this isn't isn't necessarily the way that you would do this. You would normally put this in sort of its own file and then included into kind of your your base files to start off with or ah, some way included into your your application. So ah, but again, I'm just kind of showing you it in a simple way here. So again, SPL lot of auto load register is going to run the callback auto load. And what it passes in is the class name. So this is why this is important because it's going to pass in the classroom. Remember that the class name is this full name right here. And it has the back slashes like this the not the forward slashes. So if we tried to what? What you would probably try to do right off the bat is just do something like require once . And then, um class, name God, PHP. Something like this. That's a pretty standard thing that I think a lot of people would just say, Hey, you know, we're gonna put and then you would create files like this app lib Juan lived to like I've done over here. So we have app liberal in the two, and you would say, OK, I'm good to go, but or maybe you won't even think to create these these files like this, right? Maybe that's, you know, maybe that's the thing. So what you'll notice is that says it won't open it, and the reason it won't open is because of these. Slash is right here. So we need to make sure we're gonna do string replace, and we need to change. We need Teoh change those into the right kind of slashes. So we're essentially going to do what we're going to search for is we're gonna search for these forward slashes. Now, the thing about this is, if you just do this, you'll notice it makes our code all wonky. And that's because a backslash is also in escaping character. So it's actually escaping this last quote right here. Eso we need that actually put two in there like that that makes sure that we don't have the escaping problem, and then we're gonna replace it with an actual forward slash, which is what we need. Okay, so if we reload that now we get are expected output, okay? And so what's happening here is when we say, Hey, we want to use apple liberal in my class. What PHP does? Is it it? It looks for those. It looks for that designation in the files that it has available to it that have been loaded. Well, obviously, if we don't require those in than it's not going to find them because we haven't required those files in. So what it does, it does one last attempt, essentially to find those files, and it's going to try and auto load them. And so this is where we can add in this auto SPL auto load, registered this callback function to tell it to give it help and tell it where to look. And so we're telling it essentially, because the name of the classes ab slash liberal and slash my class what we're doing with this. But this function here, auto loaders were saying, were first off for changing the back slashes into forward slashes. And now we're telling it, Hey, look for this class in app slash lib one slash my class dot PHP and look or look for it in lib. Ah, ap lived to ah, your class dot PHP That's where we want you to look for this. So we're telling it where to look with this now, that's important, because okay, maybe you don't want to. You don't want to use app and live one like this. Maybe you want to do something like let's go. Ah, let's go parts equal explode and we're gonna do our backsplashes again because we need to explode by those slashes, and we're going to use class name. And we know as a result of this that the last element we know what we're looking for. Now, this is a little tricky because you're assuming that they all have the same structure. And you may have designations like this that are like, um no other saying, I don't know what this might be. You might have four like that. Okay, so you got to be a little bit careful, but you can use ah, something like, ah, Ray Pop. I believe it is that will pop off the last element of an array. So ah, and it's going to return that the last value of the array as it's returned parameter so we can do ah named the actual direct name equals, Ah, array, top and parts. All right, let's just, ah, echo that out and make sure that we're getting what we're after. All right, So let's go ahead and do that. So now we're getting my class. So that's one of the ones that were after so again for each one of these that it tries to load. It's going to run this because it's not finding it. So it's gonna run it for this one and then run it for this one. So now we've got my class encapsulated. And even if we did se other here like this, we're still going to get my class because we're using a ray pop. So now that we have the direct class name, we could create a folder like this That's, Ah, new folder and we could do classes. And we could put all of our classes in here, and then we could construct this so that it looks for it inside of that so we could do, um let's get riddle this and we could maybe do something like, um, let's do a sprint f make it a little bit easier here. Those do sprint f. And the format is going to be Ah, we're gonna look for it in classes and then slash and then percentage s and then dot PHP. Okay, that should work. And so we'll do name will put name into their. So what sprint half does? Is it were this percentage s It's gonna take the value of name and drop it into their Okay , So it's just a little bit cleaner way of doing this. I may need to add some slashes and stuff here, but let's see what we get. Um, no, I got my echo and exit up here still. I mean, it may need to add some slashes, but let's just go and see. Yeah, So let's ah, well, And actually, we don't have my class over there, So let's do this. Let's do Ah, copy. And then let's paste this in here and then let's do your class. Let's copy that and let's paste and I'll get to the point all this here in just a sec. All right, so there, Now we go. We've got it right. So we've done. We've just done two different ways of auto loading. We've done it where we create app and lived one like this, which you might see some people do, or we've done it where we've put all of our classes into one folder and no matter where they you know where they're going to get called from, No matter you know how many different levels we have here. Ultimately, this auto load function is going to look for our classes inside of this class is folder, and if there is one, it's gonna load it. Okay, So I show you all of that to point out that when you go out there and look at different applications, you're going to see different ways that people might auto load stuff. And you might start to think that's the only way that you can do it or whatever. But this auto load Ah, function. This register allows you to call a callback function, gives you the class name, and then you can parse it and handle it however you would like. And so that allows you to build your structure in a specific way. That that makes sense for you. So you can kind of organize your father your your application however you want. And the nice thing is, is we could have, you know, 17 of these use. Ah, these use designations here, and we could have 17 different classes. And as long as your way you write auto load, you make sure you organize your files in that particular way so that it works, then you're gonna be just fine. And again, that's gonna be a little bit when we when we get into Ah, when we get into start looking at NBC and we're gonna look at some of this stuff, you will see some auto load functions that have a lot of They have some if blocks and they're a lot more complicated than this. And and that's fine. And that in actually works pretty well in those scenarios and so forth. But I want to show you this so that you have a base understanding of what's actually happening there and to let you know that you can kind of write your auto load however you want. And it's a really handy tool for when you have a lot of classes that you need to load instead of putting 17 require blocks at the top, you can use an auto load like this, and you can use if statements in here and so forth. And so maybe you would. The way you organize it is you would actually check. You're doing you doing explode. But you would grab the first element off of it, right? You would grab this app part, and you would say, OK, if its app I'm gonna look for I want you to look for it in this folder, and you could have an if statement that handles if the If the first element of this designation is app and let's may say, maybe it's I don't know, maybe you have, ah, YouTube, STK or something and you want to use YouTube or you have a YouTube rapper. And so if it's YouTube, I'm gonna look somewhere else. Or if it's, there's all sorts of different ways that you could organize this. But again, I want you to know that you can write this function how you want so that you can organize your application, how you want and still be able to take advantage of it. So that is auto load. Hopefully get something out of that. Thanks for watching. We'll talk to you next time.
9. Interfaces: Hey there, John Morris Here, Joe Morris online dot com. This lesson. We're gonna talk about interfaces, so interfaces are a way they what they do is they allow you to specify kind of the framework that a class must implement in in particular, what it does is it tells you the the methods that a class has toe have if it's going to implement. Ah, your interface. And this is handy for a number of situations. For example, let's say you just want to be you as an individual coder writing stuff. You want to be more organized and kind of keep yourself on track and so forth. And so what you can do is when you are, you are building out sort of the idea of what you want your application to be and getting sort of the base sort of ideas and concepts down and functionality down. You can create different interfaces that say Okay, I know that this this interface needs to the that thes Siris of classes are are going to need to do some sort of data manipulation, so they're gonna be storing stuff in the database working with the database, so I know that I'm gonna need to have a create a read and update and delete so you could write an interface that says, Okay, I need to. For every one of the classes that implements this interface, they need to make sure at a minimum, they have a Cree Cree read and update and delete. And so then when you you write the class and you implement the interface, if you don't have those methods in there, then it's going to throw an air and you'll be like, Oh, yeah, I need to do that So that's one instance probably a more popular or are realistic instance is Let's say you are the senior developer at some sort of organization and you are tasked you and your junior developers. Maybe you have final five or 10 of them. You are tasked with creating some sort of new application. And so, as the senior developer, you want to provide sort of guide rails for your developers and make sure that their ah, they are writing the necessary things that you need for the application so you can sit down and write a series of interfaces and lay out the basic framework of what the code needs to do without actually having to fill in and write the code. And then your junior developers need to. When they write a particular kind of class, they need to make sure that they implement a certain type of interface Ah, in order to for it to work. And so that gives you a scenario where, as the senior developer of the project manager, you can sort of guide the development and set up a framework for the development, but not actually have to write the individual code for each class. And so this is what a an interface does. So to create an interface, you just used the key word interface instead of the key word class. And then you give your interface a name. You have your curly brackets that it's inside of and then inside of it. All you do is specify the methods that this interface must contain, so we can see we do public function, and then we have one name do thing, and we have public function and one named do other thing. You don't put any data inside of the method. It won't let you it'll you'll get theirs if you try and put data inside of these methods all year, specifying is the names of the methods that have to exist in any class the implements, this interface and so you can see down here than we When we want to implement the interface , we say class my class instead of extends. When we were using a parent and child classes, we do implements. And then we say my interface when we do that, what this then says is now we have to make sure in this class we have a method called do thing and called Do Other thing. And so if we refresh this, you see we're getting no output over here cause my output, anything. But if I come in here and let's just go ahead and comment out these last two methods, So now I have do thing, but I don't have do other things, and so if we come back over here, I get a fatal air class. My class contains one abstract method and therefore must be declared abstract or implement the remaining methods, and then it tells you the method it needs to implement. So again, if you're if you're in an organization and you're the senior developer and you have a bunch of developers working for you and you say you go to them and say, OK, I've implemented all the interfaces. Here's the interfaces you need to use for this this and this. And they say and they go to their code there. The guy class implements my interface and then they write, they try to load the page, you'll see. Oh, you need to implement this one you need implement this one you need implement this one like they have ah, guide rail of These are the things that I need to complete. And so then they can go through and complete them. And when they go to show you their code as the senior developer, if they haven't instruct, ah created one of the required methods. You're going to see it right on. You're going to see it. You can try and load it, and it's going to say, Hey, this method doesn't exist so again and gives you some control and some guidance in terms of the general framework of what you're building. Now if we come down here, of course Willen comment this one and you can see now we get no output because we have both methods defined. The other thing is, it's not just those two methods you have to you have to implement, or you have to create the to all the methods that are specified in the in your face, but you can add your own methods to it. So now we have 1/3 method here that wasn't outlined in the interface. But you can see we still don't get any errors or just fine with that sort of thing. So again, this is This is creating an interface that allows you to get some control on some structure over the classes that you're creating. Um, and that can be used in a number of different ways, whether it's you personally or working in a team and so forth. So that's what interfaces are. That's what Therefore Ah, that'll do it for this lesson. Thanks for watching. We'll take the next time
10. MVC Primer: Hey there. John Morrissey, John Morris online dot com. This lesson This is gonna be a primer on M V. C. Now I want to make sure I'm ah, clear on what I mean by by primer and what we're gonna be talking here. So primer is designed to be a a sort of top level view overview of concepts. So we're not going to get into writing any sort of code with this. Not not Ah, you know, going into our editor and and laying all this stuff out This is gonna be an overview of concepts. And I've included this even though it's doesn't really necessary. It's not really 100% object oriented programming. I think, ah, that a lot of people, when they think of object oriented programming, ultimately are kind of pushing towards a sort of design pattern because ultimately object or any programming is about application building. And so NBC is a really popular design pattern, but it's not the only design pattern. Ah, that's out there. And really, if you want to learn NBC in depth as that, that is your design pattern than you would want to consider taking a an M V C specific course because it is big enough of a topic that it warrants its own sort. Of course. So I want to give you a primer in an overview of of of high level concepts so that you understand M. V c and and understand that it is one among the many of the design patterns that are out there. But it is also a very popular one, and so we can talk about some of the stuff that's happening with it. The other thing kind of tomb or sort of caveats. One. I'm gonna be referencing this article here over on site point and I wanted to make actually come here and show you this because A and I will link to, ah, this this article, uh, wherever I can in terms of where you happen to be viewing this course. But I will link to this article so that you can see it. But there's sort of two things about this article one I really wanted to use. Ah, I really like this picture right here, and I didn't want to just swipe the picture, so I wanted to show you where I actually got it. from. We're gonna talk about this as we go through this. The other thing is this. This article makes an argument that I happen to agree with when it comes to NBC. And the argument is that a lot of the frameworks that you're going to see today if you go out and you look at, ah, the different frameworks or people that are using the NBC pattern today, a lot of what you're going to find is sort of a a an alternate use of what NBC actually is . And as this article sort of makes the point towards, it's not really true. M. V C. And we'll talk specifically about why that ISS and so and that doesn't make it wrong. Ah, what's kind of happened is people have developed their own sort of design pattern. But it's important to understand that what you see out there from a lot of frameworks, really is a different design pattern that implements similar ideas but in a different way. And so if we really want to talk about pure sort of true M v C, I want to make sure you understand what that is. That's not saying you can't do what these other frameworks air doing, and then you have to do it this way or one's better. This at the other. I just want to point out that there is a distinction between the two, so you know what that is? All right. So what? That said, let's get into what this is a little bit. So NBC stands for model view controller. The whole idea is separating business logic and sort of data management and, like the back end sort of stuff with sort of front and display sort of things. And if you've ever built any sort of application, you know that there is. There can be this sort of muddying between the two, and there's only has a point in the application. If you're not doing this sort of NBC framework or some sort of design fatter and designed to handle this always a point in the application where you feel like part of it is kind of dirty. And it's like this is kind of Ah, hack around and it doesn't feel super clean. And so what NBC does is it helped tries to address that sort of thing for giving you sort of principles and rules that you can follow in order to help keep your applications really clean, really modular and so forth. So what again, what? NBC stands for his model view controller. So these air the three components and you'll see as we, uh, on our picture here. This is all related to this implicit idea of the user. So I sometimes talk about this implicit user. Ah, this is It's always assumed that there's a user kind of in the background using our application. And so the controller, ultimately Ah, actually, it's probably better if we start talking about the model first. So the model is what we use for our sort of long term storage of data. So this the model is ultimately blind. It has no connection or has any idea what is going on with the view or the controller other than what is requested of it? And what I mean by what is requested of it is that the controller will, As you see here, the controller will interact with the model and update the data that's in the model, and the view will interact with the model in order to kind of ask it questions in order to get the data that it needs in order to display what it needs to display to the user the interface that it needs display for the user. But the model really has no idea of any of that. It doesn't know what's why the data needs used. It doesn't know why it needs updated. All it knows is I've been told to update this data or I've been told to retrieve this data and return it. And so it simply is responding to the requests of the controller and the view. And that's important to understand in terms of separating out what's going on between the controller and the view and from everything else that's happening with the model. The model is ultimately going to revolve around your sort of database function, so it's going to be the thing that is going. It's gonna be your crowd gonna be inserting data into ah particular table. If it's told to, it's gonna be updating that data. If it's told to it's going to be retrieving it and returning it to ah view if it's told to , it's gonna be, ah again deleting that data. If it's told to. So it's just reacting to what it's told to do, and it's completely blind and it focuses solely on the long term storage of data. OK, so thing when you think model, it's not 100%. But just think database. When you're thinking of that, then that's when we get into this distinction between the view and the controller. Now you'll notice here there's no arrows going between view and controller, and this is where we get into this distinction between what you'll see from frameworks that are out there and sort of true M v C. In true M V C. There is no interaction between direct interaction between the controller and the view. The only way they indirectly interact with each other is through the model. And what I mean by that is that if the controller updates some data in the model, of course that's ultimately going to affect the view. But the controller doesn't tell the view anything directly, and the view doesn't tell the controller anything directly there completely separate. Now that what Why that's different from modern frameworks is a lot of the modern frameworks . What you'll see is the controller actually sort of sits on top of the view and the model. So a lot of times what will happen is the controller will control what views are being displayed. And it's simply pulling templates from the view in order to have the proper display. And that is not really true. M v c. In In true NBC, the views are more than just templates. The views are actually interacting and talking to the data, and they control that, that whole part of it. So to get really specific about how this works, let's say that you go to a sort of Web page, and when you you go to your let's just say indexed, I could be whatever domain Lissy Index, stop PHP When that page loads up, there's a number of things that need to happen in any sort of application. First off, the application needs to know what page you're on. Are you on index dot PHP? You know, if you're in a CMS, you could be an index dot PHP. But then a query parameter of P equals nine. Well, that means we're requesting the post that is has idea of number nine. That's different than being on the actual home page. So you need to know where the context of of where you're at as a result of where you're at , you need to know. Maybe I need to check for ah, see if there's any session variables to see if this person has logged in. Or maybe I need I have do have ah, query parameter that specified that requires me to query, query certain information and display things differently. So if you're on index dot PHP would know query parameter, I might pull the template home dot PHP And I might tell the data or tell the model Hey, I need all posts. Okay, I need or I need the last 10 post because I'm a display 10 at the time. So it'll query the model and say, I need the last 10 posts. It'll grab. Okay, I need the home page view, and it'll take the data and it will put it into the view. And that is what ultimately leads to the display of the page. Okay, that's all the views job. The view is supposed to handle all of that. Figure out the context. Figure out what What Dad, I need so I can figure out what view I need. So I know what I need to display to the user that all is the view side of things. The controller side of things takes over when some sort of there's some sort of interaction between the user and our application. So let's say you are on again. You're on an individual block post page and on an individual blawg post page, there might be a comment box, so there's a little text area with the submit button. As soon as that submit, button is clicked and let's just for a second forget about Forget about Ajax and JavaScript all that for a second. Let's just imagine period PHP and nothing else As soon as that button is clicked. We've now had an interaction between the user and our application, and we need to handle that. And so we need some way to know. Well, what what data was submitted, what was the context and what it was? Some it. I mean, it could have been a comment, but it also could have been the admin in the back and creating a new post like what is the context of this data so I can figure out what I need to do with it. That's what the comptroller does. The controller figure out, figures out. Okay, This data was submitted from an individual bog post page. It was a comment and it was a comment on this particular post. This it was a comment by this person. Here is what the comment was. This is the date that you know, it figures out the entire context of where that user interaction came from. And it takes that interaction and it processes it. And then it updates. Demotte tells the model What toe update? Accordingly. So it says to the model, Basically, Hey, we just got a comment I need. And it was on this post. I need you to add a new record to your database to your comments table. And this is the data Ford. This is the post that was from This is who is from etcetera. The model goes OK. Who ended up dates? That's it. It just does what it's told to do. So now that can be a little bit muddy because you can start to think Okay, the view and the controller connected because if you're on the page and there's a form button on there. That's sort of the view, but it's also user submitted data, but so it's kind of the controller, so they are kind of mixed. But the way to make the clear distinction about it is is this. The view is responsible for displaying the button and displaying the form and the action parameter in the form to to say where this is. This data is supposed to be processed at the view is responsible for displaying it. Once the button is hit, though, the view has nothing to do with how the data itself is processed. That's all of that's all the controller. Okay, so the controller takes over at that part, and then it will update the model. And then, of course, as a result of that, then the view will be changed because of the data has been changed. So again, Teoh kind of go through the process. When a page loads, let's say, an individual block post page. It's the views responsibility to look at the U. R L and say, Ooh, we got P equals nine. Okay, That means this is post I dean, this is a post its i. D nine. And so I need to grab the data for that post from the database. I need my single post view because this is a single post page, and I'm gonna insert or inject this data into this template and boom. That's what I'm going to display that includes a comment form. So that's all the view and it in that process, the view might can go back and forth with the model look and say, Hey, I got a post with the idea of nine. Does that exist? And the model can say Nope, that doesn't exist. And you can go Hope doesn't exist. That means I need my template for my 404 Boom. I display that or it can say, Hey, does this post idea of nine exist and model says, Yeah, that exists. And then you can go. Okay, send me the data for that. And now the view can go. Okay, I got data for this and how I need my single template. Ah, file. Here you go. Here's Here's what you need. That's the interaction between view and model. Okay, Now, once that page is displayed and includes the form the former submitted controller takes over. Controller, you know, says to the database, Hey, I got a comment for Post I D nine. This post I d nine exists. Model says Yeah, that exists, he says. OK, add this comment to the ad. This data to the are at a new record to the comments table referencing and it references. Ah, post I D number nine database goes okay. At that point, the database will tell the controller whether or not that was successful. It will says it will say back to the controller. Hey, that that comment you on me and insert, It's good to go. All inserted and controller Will will look for that now if the if the model says back to the controller. Hey, I tried to insert that data, but something went wrong. Here's went wrong. What went wrong? The controller. Go Hope something went wrong, and it and it will then handle that accordingly in terms of what you have set up for. If you wanted to go to a different page or whatever, it will do a sort of air redirected based on that. Okay. And so there at the end of whatever the comptroller does again. We're forget Ajax and JavaScript for a second. Whatever the age act, whatever the end of the comptroller does, there's always going to be some sort of of page load or redirect. So you may be directed to an air page or you maybe read if, if it's successful, the controller may say, OK, it was successful. We can redirect to here. So at the end of whatever the comptroller does, there's always going to be Page. There's always going to be a sort of refresh of the view when that and it may be a redirect to a different page, etcetera. But there's always sort of a reload a refresh. Okay, when that happens and that that new pages now coming online, we're now back to the view is in control. Okay, so really, the only interaction between the controller and the view is that the controller will trigger these sort of refreshes and say, we need to reload the page because our data has been updated. So that's and then when that that reload happens, then you're back to the view is the thing that's trying to figure out what's going on. Okay, We're on post I d nine again. We need to get the content for that. Let's grab all the comments. Oh, there's a new comment Boom. We populate the page with the new comet, okay? And so again, just to get back to sort of true NBC, it is the user that probably the controller that processes user input and it it communicates directly with the model and ultimately updates and manipulates the model. It is the view that interacts with the model and ultimately displays the proper thing. The proper ah, uh, user interface for the user based off of the context that the that that this is all in in terms of, what, pager on any sort of no, any sort of other session very visible might check for. And then, of course, the data that it actually requests from the model. So again, I want to give you that big picture framework for what NBC actually is and how those things interact. So hopefully that kind of gets you on the right path, and then I just want to look at some some a few different APs here. So one is from its called this mini. So this is just a zit sister. It's an extremely simple, naked PHP application. It's really met for prototyping. If you want to get familiar with how frameworks are built and you want to start toe, interact with and so forth, then this is something that you can use. There's also different forks of it. So you have tiny um, you have many have, ah, those sorts of things. So ah, again, it's a, um it's a very simple thing that you can look at. It's a little bit easier to look at something like this than to just go immediately into looking at something like Laura Bell and be like, Whoa, what is going on? So ah, again, one that you can look at. It's a little bit older, but these things don't tend to change a whole ton because they're really basic, bare bones sort of thing. So if we click into application, you can see that we have folders for the view for the model for the controller, and so if we click in here, then we have these the different views we have home and when we have different sort of files that are in there for the home page. We have problems So we have ah Index for that we have songs on We have ah display of songs Probably an edit of songs and so forth So these are all different views If we look at the model, go to model that PHP and we look in here you'll notice that this is all OK has to do with the data base Ah connecting via PDO getting all songs So read, get and reader the same thing So get all songs Ah, down here we have get song So get one song Those air Both read methods we have add song So that's insert We have updates Song So that's update and we don't have a delete But we have Do we have the lead up here? Oh yeah, We do have delete song And then we have get amount of songs So we have three read methods. We have an update. We have a delete Ah, and we have a an insert or create So again, back to my point that the model is deals primarily with the database interactions It's really comes down toe cruddy it and then anything sort of auxiliary around that or or different types of crowds. So you have three read methods and so forth that you might need to do. But the database or the model is solely focused on the data, the storage of data, the updating of data, the the retrieving of data, that sort of thing. Now again, you know, the the sort of of if we look at the views thes are what we've sort of talked about. If we look at home and we look at index dot PHP, it really is just sort of a template. And then if we go into, ah, controller on, let's just go with home, you can see that the controller is loading different views. So this is what I'm talking about when you look at some of these more, uh, these frameworks that are out there is They put the controller on top of view, and they actually have the controller loading views and figuring out what views toe load. And that's really not the way, like kind of pure NBC is designed Now again, I'm not saying that's bad. I'm not saying one's better than the other. I'm simply saying that the way that a lot of these frameworks has gone is sort of an alteration of NBC. So that's the sort of thing that you're going to see. But you still do. You do see, here we have model. We have you. We have controller. Here's an example of another one. So we have this little more basic. We have views in here pages, post users. So all the different sorts of views that we might have. Um, if we go into pages, we see the different pages that we have. Um, we're going to models. What's going to user? So this is a cold class devoted to working with the user, as I've kind of mentioned before. This is a whole class, ah, meant to deal with handling posts. If we go into libraries here, we have controller whole class for the controller. Uh, we have core. We have a whole class for the core. Here we have database. So a whole class for interacting with the database. And so again, we have models, views, controllers. We have this helpers area here. And then if we look at bootstrapped out PHP, This is where you can see our SPL auto load register class name and he's going with require wants library stock. Class name. So this is for dealing with the the ah classes that are in the library. That's what this auto loader helps dio. Now this doesn't seem to be working with name Spaces at all, so that's something to pay attention to. But again, just a couple examples of different sort of, ah, applications that you might see your work with out there and how they try to implement M. V. C. At the end of the day, you know it's your application, and you know the way you want to implement it is how you want to implement it. I just want to give you a sort of primer to help understand the main concepts, and the thing to understand is, and some people might disagree with this and say No, it's been figured out, but again, going with what we've talked about about the distinction of, you know, kind of true NBC and what we see out there and frameworks, I don't I think that's up for debate. But ah, lot of people are still trying to figure this sort of thing out, right? It's It's not like there's this hard, fast way. This is how you do it. All right, there's there's still some, especially as your application gets more complex and has asked to handle more things. It can start to be a little bit mawr more difficult to keep those sort of separations and so forth. But if you get the base concepts in the base idea and understand, you know that that controller has met for dealing with user user input and updating the model, oh are interacting and manipulating model. The view is is meant for, uh, displaying the user interface and the data needs and figuring out what template it needs to display and all that sort of thing in that interaction. And the model is completely blind. All it does is react to what's requested of it. Then that kind of starts toe. You could start to figure out how to how to separate out your application in any way. That sort of makes sense, so hopefully that gives you a little bit understanding of NBC in some of the top level stuff. Like I said, this is a primer. If you really want to dig into NBC specifically as a design pattern, one among the many. It is a popular one. It is a good one. But it is one among many. I would recommend no finding a full on NBC course and sort of diving into that and keeping in mind some of the stuff that we've talked about about they will, the way they may teach you. Ah, NBC. It may not be the true sort of NBC that we're talking about, and that's completely fine. If you're fine with the way it has been taught to you, then then by all means go that route. So all right, that will do it for this lesson. Hopefully, you got something out of that. Thanks for watching. We'll talk to you next time.
11. How to Build Professional Applications With OOP: Hey there, John Morris. Here, John Morris, online dot com. This lesson. I want to try to really give you the the help or the the direction, the insight to be able to start building your own professional application based off of everything you've learned up to this point. So there's a couple caveats going into this. First off, I think the biggest one is what I'm going to show you here is not some. This is something I saw a kind of a version of once, and I've kind of kind of added my own flavor to it. But what I'm showing you here isn't necessarily sort of the way everything's do. Everybody's doing it now or I'm not. He wouldn't even say go so far as to say this is necessary the best way to do it. What I'm really trying to get at by showing you this is to try and sort of stretch your mind and get you to think about everything that we've learned in in different ways and to help kind of spark your creativity. And there's kind of an important reason behind why I want to do that, because I don't one of the things that I think a lot of people get caught up in, and it's really, really good programmers to is these that there's these hard, fast ways of that. We have to do things. You've probably seen this all over the place where you know NBC comes out and everybody tells you you have to do NBC and you look at you know what's going on in our space right now on dear starts to be sort of this standardization of the way everything is done, and part of that is good. You develop best practices and you wanna know to build the best application. You rely on best applications and so forth. But one of the problems that I I think that leads to is people look at a best practice they start to look at. It is like it's the only way. And if you don't do it this way, while you're just an idiot and I really try hard to push back against that because ultimately I don't want to create students that know how to do it, who just know how to do what everybody else is doing. I want to give you that which is. Why did the NBC primer. But I want students who are not followers, but leaders who are the creative people who are constantly looking at things and saying, How can we do this different? How can we break this apart? How can we make this better? And are the ones that air creating the new ways that that become the best practices? That's what I really want to try and give you. And I think the way to do that is to stretch your mind, give you different perspectives and show you different ways of doing it. Even if you might look back on, you know, we might all look back on this five years from now and be like, Oh, that was really dumb. You have to be willing to risk that. You have to be willing to risk falling on your face in order to do something great. And I know I'm getting a little personal here, but I really, truly believe that. That's why I'm even making this video in the first place is to hopefully inspire you and get you to think about this and a little bit different way than maybe what everybody else is doing so would that said I want to talk about this idea of building truly professional sort of applications and the way that all works. Is it to me that it always starts with the database? So in order to talk about that, I'm gonna hope Open up this install found Don't worry. We'll get to some of the what you're seeing here with this index and so forth. Everything that we're gonna talk about ultimately leads to this. The storylines right here and the simplicity of it is really what we're after. So But again, it all starts with the database because that's where the data stored. That's really in most applications. Ultimately, the most important part of your data gets screwed up. Then you're gonna have all sorts of problems. And those problems are a lot harder to correct because then you don't know what data is, right? If it's a code air, you can use the data to help you figure it out. If it's a data air, it gets really hard to know what's what. So I've talked about this in some other courses that have done, but I want to cover here again because again. I think this is a the foundation for a professional application. So when you're creating an application, there are again. Generally speaking, there could be caveats to this sort of thing. But most of the applications you're gonna build there are three primary types of tables that you're going to want to create. And so I want to talk through each one of those and how they all sort of work together. So the first sort of table that you're gonna build is what's called in what I call an object table. And this is where you are going to store your objects in the data about them. So in our tables array right here. And I just used these as examples for kind of a starting point for you to think about how to build your database, depending on your application. This will, of course, change. And one of the things that I don't want to do in this video is give you all the answers. This is more of a challenge. Video this video air. This lesson is meant to not give you all the answers, not give you all the code and challenge you to write this stuff and figure this stuff out yourself. That's how you truly move into being a professional developer. But in our tables array here, this is We're basically that what I'm saying here is these air, the different tables that we might have in this particular application in that the post and the category tables would be our object tables. So if we come down here to example for the post table, what we're going to store in those tables are the data, and I want to be specific and used the word data and we'll talk about the difference. What? That Why that needs to be specific. And second. But it's the data that ultimately kind of forms the object. So a post is not a post really? Right. If it doesn't have a title, if it doesn't have some content, you know, if it doesn't have a slug. So the and and and all of these could be debated, of course. But what we're saying when we create our application is that a post is not really a full post unless it has thes things. So the object table is going to store that data and what you what again? What is the two things that I think are unique about an object table? Is that or an object? Is the data that is that we're storing is both required and will put an Asterix by, required in the sense that you can get away with not having some of this right? You can create a post without content or post without title. It's not like you can't do that, but it's not really a post. So in order to make it what it's supposed to be, this data is required, and it's, ah, data that ultimately helps to form the object. So if we're thinking about our analogies, it's like the wheels, right? You can't have a car without the wheels. Technically, you could have a car and it would be sitting there. But it wouldn't do what it's supposed to do if it didn't have the wheels. So the data is like all of those components that it has tohave. Okay, so and you got to be really ruthless about this because you may think that something ah object has to have something when really it doesn't. So again, title content, you know, maybe a featured image would be one that you say is required. A slug you is the developer. Get to decide this. Okay, so that's what an object table is. It's the data that really forms the object itself. And you can see down here under category. We have similar types of we have I. D. We call it name and description, but that's really similar to title on content and then a slug as well. So our post and our category tables are object tables. These are primary tables, these with ones of Neil that we're storing all of our different objects that we create in next we have metta tables. So meta tables are we talked about the object table store the data, the meta tables will store the meta data. And so again, metadata has kind of two distinct features that are kind of, in contrast, of the two features that that objects have and that is meta data would be stuff that we consider optional s, so it doesn't really necessarily always have to be set for every for every object. And the metadata is usually stuff that describes the object that describes the object or the data that makes up the object. It's not the stuff data that actually builds the object itself. Okay, so to try and give an example of that in WordPress, there's a plug in that he used cold Yost s CEO. And one of the things that Yost s CEO does is when you create a post, which is an object in WordPress, it adds a little box for you to input a focus keyword. And when you input the focus, keyword, What it does is it analyzes that your title, your content, your tags, all this information about the object and gives you a score and then gives you suggestions on things you could do to make it. Morrell seo friendly. So that focus keyword, I think, clearly fits into the ah, the the scenario of metadata, right? The focus keyword isn't We don't need that to create the object. It's not a part of the object itself. What it is doing is it's more describing the object. So it's analyzing it and give us giving us a sort of score. And so it's it's describing, or its data about the data, which is what? The kind of the definition of metadata and then it's also not necessary necessary. If we don't put in the focus keyword, we still have Ah, the object object can still do what we want it to do. Um, we just won't know what the what the CEO who kind of score for it is so that's the difference between the two and you kind of. It's not a necessarily a bright line to use a common phrase these days. Ah, it it's something that you kind of gotta go back and forth on and try and figure out now with that said, when we want to store this sort of data, it actually can be a little bit sort of difficult if we're trying to put that data in the same table as our object, right, Because maybe we, you know, if if if we're gonna have, ah, certain particular a certain piece of metadata that we're gonna add to the to the actual object table, well, we'd have to have a column for that. And then we might have 99 posts that have don't have that metadata and one that does. So what kind of starts to be a little bit of waste of time. Plus, when you start to consider what we might have 10 pieces of metadata. And so now you would have to add. If you're going to store it in the objects able, you'd have to add 10 columns, and then you'd have to store the data about that, or you'd have to create a sort of meta column. And then you have to try and keep insert key value pairs, which means you'd be kind of working with the Rays, which means you have to serialize that, which means you then couldn't search it. And so it starts to get a little convoluted to try to do it that way. So the way that we do that the way that we handle metadata and make it modular and scalable and flexible is we use a meta table, and so a meta table has three primary. I'm not saying these are the only things you can have, but these are the primary things that you need in order to make this work. So this post meta designation here has three main columns. It's gonna have the object I D. So in this case, we have post. So it's the Post I d down here in category Meta we have category i d So has the object. I d has a medic e in the meta value. So what we're essentially saying here, when we create a record in this table, is we're saying for this post with the I d of let's say one, we want to attach this metadata to it. And the key, the name of the metadata is to use our example focus, keyword. And then the value of that is, let's say PHP. And so what that tells us is that this post i d the focus key word for it is p PHP. And now we can add all sorts of records to this. So we get at another another record in here. We could use the same post idea of one. But now we have a different key, and we might say this is S e o score, and then we we the value of that is 90. So now we have the same post, has two different pieces of metadata, and we could store 15 pieces of metadata about that particular post, and it's not making our database any more cumbersome or anything like that. It's very modular, and we could store no. Ah, the S e o. Score on the focus, keyword for post number with idea two and three and four and etcetera. Okay, so and then when we need to grab that data, what we can do is if we're in a before, there's a couple ways that you can handle it when you grab when you're grab, let's say someone visits an individual block post page, and you want to grab the post data to display it. When you grab that data, you can automatically grab all of the post meta if you want. And so the way that you would do that is you would go. You would query the post meta table and you would have a where clause and that where clause would be where Post I d equals and the idea of the post that you're going to display what that would grab from the databases. All of the records in this post met a table that have a post i d. Of one or whatever the post ideas. And now you would have all that metadata that that queried that you could then work with and display how you want on your page. You can do it that way Or maybe a little bit more efficient. Way is any place that you want to see you again You're loading a page that displays a block post any place that you want toe display a certain piece of metadata you can just whatever the metadata is that you want to display You can query the meta table for it so you can query the meta table and say, Okay, I got post I d one and medic e you know Ah ah, Focus, keyword. And so now you're just querying you're quitting for its the Post I d and the Medic E and you're just getting that one record. And so now you pull that one record and display it where you want to. Instead of pulling all of the data, whether or not you're going to use it, you just pull the one thing that you need. She can handle it kind of either either way there, but it makes it really modular for you to be able to pull this data on display and and so forth so that you're made a table So you haven't objects table. You have a meta table. The last sort of table that you have is a relationships table and what the relationships table does. Is it essentially ties to objects together? So the context of this is based on the application. So, for example, in a CMS you might put a post into a category. That's a pretty common thing that people want to do. Well, the way that you do that is through relationships. Table Ah, in, say, a social network. You might want to make one user a friend with another user again. You do it the exact same way with a relationships table or any e commerce. Ah, application. You might want to say, This user has access to this product again. The way you do that is exactly the same. It's a relationships table. So again, the context, the relationship, the way you store in the it's stored in the database. I call this a dumb ah, dumb relationship. The database doesn't know what the relationship means, and you could add some data to this table to maybe, if you need to. Teoh help provide more context. But generally speaking, the database doesn't know. It just knows that these two things have some sort of relationship. And so we create a table called Post Category and in in that we're gonna have this. What's called post category i D. So that's not the Post I. D. It's not the category d It's actually what it is, is the relationship i d. And that's just handy for if you need to delete relationships and so forth. It's a little bit easier to query that way, but it's actually not, ah, 100% required to have this, but it's a good thing to have. But the two main things that you have in here is you have one. You have the basically the two objects that you want to connect. You have the post I D. And the category i D. Which references this i d. This I d call him from our post and from our category. So if we want to put ah post with the i d of one into a category with the idea of one our relationships table with literally in these two columns, you would have post I D. And it would be the value would be one and Category D. The value would be one that creates a relationship between the two. If you wanted to put a post with idea four into a category with the idea five. Your relationships table in the Post I D would have four as the value and in the category. D would have five as the value, and that creates a relationship between the two again. You can create them. You can update them. You can really read them. You can delete them just like any other table. And so that's how you actually put ah post into a category or tag a post with a certain tag . Or add a user to a product or make a friendship between two people in a social network. It's tying to objects together, and you do that through relationships table. Okay, so that's Ah, that's a lot. But the databases is super important, and it's important to understand how to build us in a modular way like this. So for any object that you want to create, you're generally speaking, gonna have a post and a post meta table. You don't necessarily have to have them at a table if you're not gonna store any sort of metadata. I would say most applications as they grow, you'll eventually have this, but you don't necessarily have to have it. And so but again, for every object of a post post met a table, generally speaking, and then for every sort of every two objects that you wanna Ah, you want to tie together, you're gonna have a relationship stable. So putting categories posts into a category, you'd have other Easter ship table, Um, making a particular user the author of a post that that one actually is a little bit. There's a little bit of fudge room there because, like, if you work, look at WordPress. The author is actually in the objects table, and so then they just specify the idea of the author and the objects table. You could do that. We're in an instance where you're not going to, um, you know, in that sense, it makes sense because an author is really, really important. And you're, you know, in WordPress you you don't have multiple authors for a particular object. So you're not tying. You're not having to tie Ah one object to multiple other objects like you would when you have a post and you want to put it into multiple categories, that's where it starts to get tricky. So in that sense, it kind of makes sense. And that's why I say you got to ride this fine line, Um, in all of this stuff. But generally speaking, any time you want to tie two objects together, you're going to to have a relationship stable for it. Okay, so that's the database part of it. Now the other side of this is we now have the code side of it. How do we support all of this and kind of make all this work together and and do it in object oriented way? So I've created over here. You'll see how this folder called Nano. You're gonna get this in the download for the source code. One again. Caveat. Going into this this I specifically wrote this not to give you all the answers. I want to re emphasize that this is not something that you could just drop in and start using. It's going to require you to understand it, to go through and write out all of the stuff. So again, this is a challenge video. Teoh challenge you to get out there and actually write this code not just use sort of what I'm giving you. So, um, this will just it's not gonna The only thing it will do is what you see right here. And so you whatever you want it to do, if you want to use it, you gotta build it into the system. Okay, so we talked about objects. We talk about storing objects in our database object tables. We talked about encapsulating in previous lessons on doing an object orange in way of programming. So now what does that look like now that we have our database set up? So I want to start here, and the first thing I want to show you is that for our two objects that were primarily working with your post and category, we have two folders in our library or lib folder category and post inside this. Then we have these different files and so you can see each one. We have post post view and post service category, category view and category service. So, again, I'm not building this in a kind of true NBC way. I want to show you something different. And this is something I had kind of seen before and kind of developed my own thing from it . And I actually I mean, if I'm honest, I actually like this a little, a little bit better. Makes more sense to me in my head than, ah, true NBC thing, but that's for every developer to site on their own. But let me go ahead and get rid of this. The way this works is we have each one of these files is a class, okay? And if we were to relate it to NBC, essentially, we have our post view that's gonna handle our views. That's pretty almost pretty similar to what you would have with a new NBC set up. We have post service, which would be a lot like the model. So this is gonna handle all of our sort of database stuff that we need to do. It's not querying the database directly, but it's all of the stuff that sits on top of our database class to do all the clearing. So getting all of the posts, updating posts. Ah, the leading posts, creating posts, all that sort of thing. All that's gonna be in this post service class and then our post dot PHP class is sort of our controller. Okay, so it's going to be the thing that sort of directs traffic and make sure that we're querying for the right things and so forth based off of the data that it gets. So, in a way, it's kind of it's similar to an NBC set up. It's just the way this is laid out is a little bit different. And probably the biggest thing is if I open up all these files what what we've done is so we've we've inherited. So if I look, if I look at post dot PHP class post extends post view if I look in post view, post view extends post service, okay, and then post services is on its own. So now the way that the way this is implemented and the way you think about it or actually backwards, so I don't want that to be too confusing, but I think it will make sense here. So you remember from our inheritance lessons that any any class that extends another class the one doing the extending is the child of the one being extended. So Post View is actually a child of post service, and Post is actually a child of post view. So if you look at it post services, actually, the parent post view is the next child, and Post is kind of the ultimate child. However, the way that we use this post ends up because it's extending view and views extending service. It ends up inheriting all of the methods from boast, service and view. So this post, this ultimate child can call any method, and the reason we do that is just to make it a little bit easier. If we come over here to our Nano, you can see we're creating two new instances of these ultimate child classes for both post and category. Now we can run once we have this post in stance. Ated in this nano class, we can run any method that's available to us in post or in category, instead of having to instance e eight view and then instance e service. By extending them, we just have access to all the methods. Now I would imagine that somebody who uses NBC a lot and is a big NBC proponent like, just fell out of their chair and had a heart attack because that's pretty, uh, is pretty blasphemous. From what I gather from the NBC people that that's not something that you want to do. You really want to have them separate. I personally prefer it this way and in the way that I do things, but it can lead to. If you're not careful, it can lead to things being a little bit muddy and you putting thing because you know everything's ultimately in, you know, ultimately available through this one class, you might not be as big of a stickler about what you put in service verse what you put in view. So you have to make sure you keep yourself organized and understanding in that sense. But I just I like being able to have access to the entire set of methods that I need, Um, when I want to work with it. So we do this both for again, Post and category. So categories The same category extends category view category view extends category service and category service is kind of on its own, and so again, in service, you would put all your sort of your business logic, interacting with your database class and so forth getting all that in. Ah, and this is again a lot like the model in view. You know, this is where you'd be handing all of your views and your displaying and so forth, creating your form forms and displaying your forms. And then category was kind of your controller. It's ultimately we're gonna have the functions that when any sort of form data is submitted , it's gonna have all the handlers. And it's going to interact with service in order to update the data in the database and so forth. Okay, so that's how this is all sort of broken down. The last thing you'll notice is that this implements service, and if we look at category view, it extends and implements view. And if we look at post post view implements, view and Post Service implements service. So both of these both the service classes in post and category implement this service interface, and both the view classes in post and category implement this view interface where those at that's what this is right here. So we have service and here's our interface and view. Here's our interface now, I just did that to kind of to show you how you might do this sort of thing and use the interface. You might not Ah, you know, it's not necessarily necessary that you do this, but again, this is one of the ways that people would use interfaces because now, if we look at service, for example, create, read, update, delete. If you don't have these four methods in your any of your classes of implement this interface, you're gonna get in air. So it kind of gives you a guy line in and framework for which you can can build from. Okay, so that's our that sort of our classes here. The other thing is, now, you know, we're we're starting to sort of implement all this sort of stuff. It's like, Okay, now we've got to start dealing with how we're gonna include and require and all that sort of thing. So another thing if we come over here to Nano Nano is like our our master class, right? It is going to pull in all of the other classes from our library, and actually, one thing I want to show you two is if we come to post service and post view. You see, I have name, space, app, slash library And here I have name, space slash Ah, name states ab slash library. It's the same name space. You could do that. You can do the same name space across multiple files. And matter of fact, if we go into category view, it's the same name space. Okay, so we've named Spaced all of these classes. And the reason why is because they were using pretty Jim generic names for these categories service. So if there's other applications, we've got this all properly name spaced, and it just helps. Kind of helps us mentally organize everything. And so all of these classes category Serve Category service. Ah category view category post post view Post Service All of those air in the same ah sort of name space. So then, once we come over here to our Nana Nano class, this is sort of our short of masterclass. That kind of brings everything together. Then I've done a couple things in here for you to to kind of look at here and get an idea of different ways. You can use some of the stuff that you learned in the course. So I'm requiring our conflict file real quickly. If you go into this Conflict folder and open up our conflict file, it's just our database details. I added in this air reporting to show all areas You would, of course, want to remove this for production. But this is a standard. Ah, conflict files were including that were also including our database class here. And if we close these up a little bit if we look, we have a database dot PHP here. Now, again, this is just a sort of blank container class. But this is where you put all of your all of your database stuff, and so I've kind of got this little bit set up, but this will pull from our conflict file so it doesn't match what's in our conflict file. It'll pass it into the constructor. And, like I, you know, I've done I don't know how many different tutorials and so forth on database classes, but again, you don't have to use create your own. You can use something that's out there that you want to implement into here. But the point here is that it's completely self contained. So it's its own thing. It's not integrated into any of the other, um, any of the other classes. And so the other classes can use it in a modular sense, and that gets important. And so some of the stuff we're gonna get to hear a second but again here in Nano were just requiring in that database file, and then we're doing our SPL auto load register. So this makes it easier for us to load all of the different objects and stuff that we have in our library. This auto loader is pretty much just for the library stuff, and the reason that is is because we have category and posts. But in a true application, you you'd probably a post category tag user. Whatever else is out there that you might add in this, you might have 45 different objects, and for each one who you're gonna have Pope, you're gonna have, you know, the variation of post. You have a view, you're gonna have a service. So if you've got five different objects and you've got three different classes, you end up with 15 classes. So an auto loader for that kind of makes sense. And so we were doing this auto load register here. And what we're calling, though, is not a function. It's actually a static method. So this is one use of a static method. Remember, like static methods are kind of ah, with the new stuff that we have with interfaces with, ah, with interfaces and names, face and so forth. Static class static functions and methods maybe don't have as much use. But here's one scenario where they dio and that is we can use it for an auto load method. So instead of putting this function outside of our class, weaken dropped right into our class. It doesn't need anything from our class. Okay, so there's nothing it gets everything it needs from is passed to it from S p o SPL auto load register so we can make it a static function weaken reference it like this in our designation to SPL auto load register. And now it's that's going to call this static method. It's going to pass in the class name, just like before I showed you in the lesson that I did on auto load and now we can write our code for doing our auto loading. And essentially, this is checking to things. So we have our object. Ah, we have our object. Classes are object folders here. We could put we could create an interface folder and then put service and view in interface . Um, and then we wouldn't need this sort of else statement right here. We wouldn't need to do some of this checking, but I just wanted to show you how you can use your auto load to, like, load in different ways. So it doesn't have to be like this, just standard format. And then you make your, uh, folder structure just match it. You can do it the other way. Generally speaking, you wanna have something that's a little simpler and make your folder structure match it. But again, I just want to show you this. So basically what this code is doing is it's kind of what we did before. It's breaking apart. Ah, when we when we in stance eight. This instance Right now, we haven't included any of these files we haven't required him in. So it's going to check. Uh, it's going to load this auto load because it can't find them, and it's gonna pass in the class. Name will. The class name is ap bac slash library Black backslash posts were breaking apart this string and we are finding we want post and that and that's what receiving as the object here. And we want the folder. So it's in the library. And so that's what we're sort of getting right here. And so what we're doing is checking to see if the folder. So this part right here is library. And the reason that I did that is because it would be possible for you to maybe make another folder Ah, directly in Nano and call it admin and ah, at start adding stuff in there, there could be different ways that you want to do this. So I just I put this in there. In this case, everything is inside of our library file, so this is a little bit kind of redundant, but it's there for you to see. But we're checking to see if we're accessing something from the library for folder. Um and then we're checking to see if we've created this array of interfaces. If the object that we get as either service or view. We know it's an interface because that's what the's, too. Ah, that's what these two interfaces are. Their names, service and there named View. So we're checking to see if it's, ah, an interface. And if it's not an interface than we know, it's one of these object folders. So we're going to require end the you know, the object standard file so that this designation right here would load, for example, post op PHP. This one would load post view dot PHP and this one would load post service dot PHP and then it'll because we've instance, even insist of category. It will also look for category, and then this else statement basically will load are two interfaces of service and view if it's not in the interface or if it is in the interface, If it's a service or view, then we're just gonna straight up grab the object. So this is a little bit I don't want to get too much into the weeds of this because it is not as important as you might think. And it's ah, no, this will be something that you would maybe change based off of how you want to do things, but the important point here is I'm able to load in in one format where I've put all of my classes inside a sub folder and lib and I'm also able to load it where I put ah the files directly in the live live folder like this. So with your auto loader, you can mix and match and be able Thio Thio use conditions to figure that sort of thing out . Maybe you don't want to do that because you want to make it that hard on yourself. But ah, it's entirely possible to dio Okay, so with that said then now the rest of our nano class where we actually are in stance, eating instances of post and category this gives us this right here gives us access to all of the methods inside of our post. Just like we talked about how their extended This gives us access to all of those methods inside of our Nano class now and so now we have access to all of our other objects inside of the nano class. You don't necessarily have to instance eighth ease in the constructor like this, right, Because if you do, it's going to load all of of the classes that you have, and you might not need all of them for a particular requester or page load or whatever. You You may only need them in specific instances. So you could selectively load these inside of other methods inside of this class if you wanted to. I'm again just doing it here for to show you brevity toe. I mean, the videos are gonna be long enough, so Ah, but this is how you instance e and use them and notice I'm referencing app, back slash library, backslash post, okay. And then our auto load or make sure that these get auto loaded correctly. So now we have access to our two main objects, post and category. And if we come over here, is going to show you one more thing. If we come over here to post view, we have this method called Hello world. It's this method that actually echoes out what we have right here and so again, the name of this class is nano. If we come to our index file, you can see what we do here is we're actually just including Nano dot PHP and that's it. We're in stance, ating an instance of man. Oh, but we're calling the Method hello world, which is in Post view so that, like I said, everything that we're doing is getting us to the simplicity of this page. I include one file, I instance e one instance of one class and I can call methods from any object that I have that I've created in this lib file. It's all auto loaded properly. I have access to all the methods so I can do whatever I want to on this particular page. So that's my sort of kind of pitch to you about another way of, of maybe building this sort of thing to help kind of stretch your mind, and I personally like the simplicity of this sort of thing. Now there are again. There are disadvantages, for example, in the way that I have this set up right now, I'm loading all objects that whether I need them or not, that's not necessarily the best four performance. So that that that's maybe, ah, disadvantage of this sort of thing. So that might be something you want to change or do differently or whatever. But again, I just want to give you another way of thinking about it, to stretch the way that your thinking about this sort of thing and and thinking about you are. Ah, you're your application building And again to show you different ways that you can implement all of the stuff that you've learned and sort of, Ah, when it comes that object oriented programming now the last kind of challenge for you If
12. Next Steps: I'm honest there's no more. So online.com, so little housekeeping to finish up this course. If you haven't yet, be sure to head on over to the class area. There is a class section for some, some steps for you to walk through for this course. So be sure to head over in that it's under the discussion in Projects tab that you'll see on the course. Also, if you head over to my profile, be sure to give me a follow on my profile here so you'll be notified when I release new courses. And I also have an ongoing sort of weekly podcast style course called Let's Talk freelance. So if you would like to have sort of access to ongoing training regarding freelancing and online business and so forth. Be sure to check out that. Let's talk freelance course as well. And finally, I do have a daily tips newsletter on my website at John Morris online.com. If you head over there, you can sign up to that mailing list. You will also be put into my own, my very own mobile app, or you'll get access to over 78 hours of free content at the time of this recording related to freelance and so forth as well. So if you're interested in that, BD sure to check that out as well. Again, that's John Morris online.com. All right. Thank you for taking the class. If you enjoyed it, I appreciate you for You. Leave me a review and we'll see you in the next course.