Object Oriented PHP

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
well hello internet and welcome to my tutorial on object oriented PHP in this tutorial I'm basically going to cover everything you'd ever want to know about object oriented side of PHP and I have a lot to do so let's get into it okay so what I'm going to be using in this tutorial is Komodo you can use a basic text editor but I just decided to use this because it's free and just to give you another idea what you can develop PHP with and you can find it at Komodo IDE com download forward slash number sign edit and I also have it in the description now before we start I thought I would very quickly cover exactly or at least roughly what object-oriented programming means now if we have a basic class man what that's gonna mean is all men have attributes like the man you'd see here he might have black hair his name might be Bob you might have an array of ties shirts pants and shoes and we also all have abilities or functions or operations that we can perform like every man can talk walk run throw eat and work now object-oriented programming allows us to model real world objects inside of things called classes they are the blueprints for creating objects and inside of those classes we're going to define the attributes and the functions or methods that those objects can do and that is the basics of what it means to be a class on what a class does and what an object is we model real world objects in object-oriented programming that's why it's so awesome so what is inheritance well let's say we have an object that is called man and we want to create a more specific version of a man called Mark well what we're going to do is mark is going to inherit all the attributes and functions that are in the man class and then we can build or change those attributes and functions in the mark class and that is basically what inherence is an overriding just means that we would go in and say override a function so that a function doesn't do what it previously had done now when I refer to functions they are also referred to as methods and operations but they are basically functions some people might disagree with me saying that but either way just to keep this basic you can think of methods functions and operations as being the same things so what is polymorphism aside from being a big word well like we just saw there mark inherited all the attributes and all of the functions or methods from the man class whenever he created the mark class now if he goes into the walk method and instead of just printing out walk he and said says he wants to print out walk fast whenever Java sees that it is getting a man object that is trying to walk it is going to print out walk to the screen and whenever it sees the mark class it's going to say oh you inherited the walk method from the man class we're going to go in and make certain that you didn't change the walk method in any way job is going to see that we instead want to walk fast with the mark object and hence it's automatically going to print that out now polymorphism can get a lot more complicated from there but that is just a basic gist or something just to understand and you'll understand a lot of how the polymorphism works and that brings us to interfaces what is an interface well basically when our class mark decides that it wants to implement an interface what it's in essence doing is signing a contract that says I the mark class promised to implement any methods or functions that are in the interface and I'm going to show you examples all these different things right now okay so this time I have Komodo on the left and I have Google Chrome on the right and I have my server set up in localhost and I'm just going to execute things first thing I'm going to do is come in and create a class well all you do is type in class and in this situation I'm going to type animal and that is going to be it I'm going to make some changes to it later of course and then inside of this what I'm going to do is I'm going to define the attributes that I want my animal to have well the very first I'm going to do is I'm going to type protect it in here and there are different ways to categorize both methods as well as attributes you can have protected you can have public and you can have private now private means that only methods or functions in the class can access and change this data public means that any can directly access and change the values for these attributes and protected is just like private meaning that only methods in the class can access and change this data however when we create a subclass like we did mark from man the subclass is going to still be able to change the value for this attribute so I want the protection of private but I also want subclasses to be able to inherit all this data so if it was private couldn't do that just stick around here you'll get it so I'm going to create a couple protected attributes and let's say that all our animals are going to have names and all I'm doing here is defining the attributes we have and let's also say that each of these animals is going to have a favorite food my favorite name favorite food protected again they're all going to make a specific sound and they are all going to have a specific ID number this brings us on to the next thing I want to talk about which are static attributes and this time I'm going to make this public so anybody can access this and let's say that every time I create a new animal object I want to automatically increment the number of animals it's going to start off at 0 because we haven't made any animals yet and what static ultimately means is that every object of the type animal that is ever created they are all going to share number of animals so if this number goes up to one it's going to go up to one for every other animal object that is created and if you ever want to access a static value and make a change to it I'm going to show you how to do this later you're just going to type in the class name with two colons and then number of animals and then you could go and add one to this or do whatever I'm going to actually show you this in an example in a second another thing we can have inside of here are constants and there's a constant and let's just say I want to put Pi in here for some bizarro reason so I'm just going to put in 3.14159 and there we go in a constant just means that it's going to be constant and it's never going to change its value if you want to refer to that once again you're going to type in the animal class name or the class name two colons and then PI in this situation that's going to you access to that and once again I'm going to show you that in a second now you're going to define functions inside of classes just like you to find functions in anything so let's say I want to create a function that is going to return the name this is a situation in which we have something called encapsulation not complicated remember when we talked about private and protected and public well by marking these as protected or private we are encapsulating or protecting that data we were saying that we don't want anybody to come in here and willy-nilly be able to change the data we're going to say instead is if they want to get the name for my animal they're going to have to call to get name function and when they do we're going to return it and we encapsulate or protect data so that they can not go in there and do things like change the name of my animal to a number or something nonsensical okay just to protect the data and as you can see right here if we want to refer to an attribute inside of a class what we do is we type into this meaning this specific object and we want the name that is stored for that specific object this is basically used as a generic way to say that we want the name for whatever the current object we are using because it's not going to be able to address the object by name so we just use this now another functions that is very useful is called a constructor and two can create that we just type in construct with two underscores in front of it and then what this guy is going to do is initialize things or set some values whenever we create every animal well what do you think would be a good value to change well we're going to need an ID for our animal that's going to be random and unique and we're going to have to increment the number of animals when we create a new animal object so if we want to create ourselves a new ID we want to refer to our ID for our animal object that we created we just go this again with that little arrow and then we go Rand and then we want to Janet generate a random number let's say between 100 and 1 million we would just do that and there we go so now each animal that we create is going to sort of have random number and let's say that we want to echo out to the scream the ID this like that ID and then we'll put some text in here has been assigned and then we'll throw a break statement in there at the end with a semicolon of course and now I'm going to show you how to access the static attribute that I talked about before we're just going to type in animal two colons and then we're going to type in number of animals and we are going to increment it because we created a new animal object another thing we have available to us is what's called a destructor now let's just put public in front of that and two underscores and destruct now the destructor is going to be called when all references to an object have been unset so if there's some housekeeping you want to do whenever an object is deleted you could do so in this situation what I'm going to do is I'm just going to print out to the screen that the object has been deleted is being destroyed of course you would never use this but in your website but whatever put a little frowny face there and there we go now remember up here where we were creating the get name function so that we could get the value of name for our different functions well PHP actually provides a shortcut way to be able to create what are called getters and setters Gators are going to get you the value of these attributes and setters are going to set the values and because they're protected that means we need to use a function to change these values and get these attributes and these little handy dandy things are called magic setters and getters and these are actually magic methods as well anything with an underscore is a magic method so if we want to define a function inside of the sky that is going to be able to get anything from the type in function and then we're going to type in git and we're going to pass the name of the attribute that we want to get to this function whenever it's called and if we want to print something out to the screen just to show that something's going on here we could do something like asked for name which is going to be the attribute they're asking for and a break statement again and then if we want to return it the attribute we can just come in here and go this and the name that was passed inside of here so we're going to put a dollar sign and name because it would get confused otherwise it would think this was the name we're looking for this is going to return whatever attribute is passed in here and I'm going to show examples of all these here in a second now if we want to set the value this time I'm going to do something a little bit more complicated just to do something a little bit more complicated I'm going to check that the attribute that they sent in here was actually a valid attribute now with this you're going to pass in the attribute name and the new value that you want to set for set attribute and let's say I want to check that the name they passed in is actually a valid attribute one thing I could do is use a switch statement here and then I can come in here and go case and I'm just like I said I'm just doing this to show you how to do it then we could come in here and say if it's name while we know name exists so set name to whatever the value is and then we can do pretty much the same thing well we're going to throw in a break statement also there we go and we could do the same thing with favorite food do the same thing with sound what's the set the sound for my animal and then finally if it's none of those attributes what I'm going to say is I'm just gonna echo out to the screen name not found and then after all that's done I get out of my switch statement I can come down here and just to print something out on the screen I could print out set name to value so there that is and that is how the little magic methods for setting and getting or or used and like I said before inheritance occurs whenever you create a new class by extending another one and like I said we're going to inherit all the attributes and methods that are defined in the superclass animal in this situation is a superclass inside of the subclass so what we're going to do is we're going to create a new class and it's going to be called dog and if we want to inherit everything from animal we're going to type in extends and then animal later on I'll cover interfaces now let's jump up here and let's create a function that we are just going to override so we'll create a function called run inside of our animal class we're in the animal class right now and let's say this one goes echo this name runs no throw break statement in there and then we'll actually just copy this because we're going to override it and as you can tell I don't need to put the attributes inside a dog that are in animal they're automatically gare so name is already inside of this and let's say that our dogs run like crazy and that's how you override a method so now what I'm going to do is I'm going to jump outside of the dog and animal class and start creating them and you can watch everything sort of execute here over inside of Chrome so I'm outside of both of the classes and if I want to define a new animal I just go animal and let's say I want to say animal one is equal to new animal and there we go and the constructor this guy right here is going to be called whenever I create this new animal and I could also pass in attributes for the constructor but I decided not to do that now what I'm going to do is come in and set the attributes for my new animal object I created and I'm just going to type in name and I'm going to give it the name of spot and then I'm also going to set favorite food equal to meat and then I'm going to set the sound that my animal makes to ruff now if I want to print out all this new data onto our screen I could do something like echo animal one name says and then call the sound for my animal and then go down to the next line and we'll say something like give me some and then would say favorite food and then let's say we also want to print out the ID number here my ID is put another period and all the code is of course available on a link in the description and then if I want to print out the ID I just go ID and then if I also want to print out the total animals that have been created go total animals is equal to and then if I want to call that static value number of animals remember I just type in the class name colon colon number of animals put another period inside of there throw two break statements in there and there we go now I can go over inside of chrome and execute this and you're going to see that the ID number has been assigned the name of spot has been assigned favorite foods meat sound is rough I called the Gator to get the name the sound and the favorite food and then it printed out all of that information on the screen and then you can also see here that spot was destroyed after the object was no longer needed this is the destructor function right there if I want to come in here and also print out the favorite number just to show you how to print a constant from a class favorite number again just type in the class name and pie and we'll throw in a break statement and there you can see favorite number printed out as well now I'm going to do exactly the same thing except I'm use the dog class I'm going to come in here and just copy all this because there's not much I need to change paste that in there and I'm going to change this to animal too and it's going to be of type dog change that to to change that too to change that to two two two two two and I can change this to dog and it's still going to be able to get it and I'm just going to come in here and change the attributes and it change this to Grover I'm going to change this to mushrooms just to be weird and I'm going to change this to Gurr save it come over here execute and there you can see we created a new ID number the name is set to grow over all those things are working Grover says gir give me some mushrooms my ID is blah blah blah blah blah one thing you might find weird hero is animals shows up as to there are still two animals created and even though we're referring to a dog in this situation it is still going to increment the numbers because it ultimately calls the constructor inside of the animal class now the next thing to talk about here is we went in here and we overrode the run class so here we have runs for our animal class and here we have runs like crazy for our dogs class so I'm going to show you exactly how that works come in here and just go animal one and if we want to run a method inside of here we just type in whatever the object's name is and then whatever we want to run an animal to in this situation and run and when we execute you're going to say spot runs and Grover runs like crazy and you can also see here Grover's destroyed spot is destroyed now what would happen if we want to actually come into our animal class and define a function that we don't want to allow these subclasses to override well we're in the animal class right now it's actually quite easy we just type in final and then if they try to override it in the dog class well it's just going to throw up a whole bunch of errors so I'm gonna allow it to happen so let's say we create a function called what is good and all it says is running is good and once again if we wanted to execute this we would have to come in here and just say what is good again we're going to use the same whatever the objects name is why don't we could just do this what is good and you can see running as good as right there so even though it's just an animal of course it's going to run inside of dog class because it inherited that another thing we could do is you see here where we're printing out all this information well let's say we wanted this information to automatically be printed out anytime the user said echo and then followed by the name of our object well we could do that what we're going to do is we're just going to come in here and copy this and then we're going to go up into our animal class we go right here and we're going to use another magic method and this one's going to be called two string and all two string does is it defines inside of here what happens whenever the user says I want to print some information out on the screen whenever somebody goes echo and follows that up with the name of the object all you're going to do is type in return they're going to print out what you want to show up except you're going to want to change all of these specific object names to this just change these to this this this and then here we're going to change this to animal of course and everything else can stay the same and then you could do something like echo animal one just type in the object's name save it execute and there you can see it printed out on the screen right there now remember when I talked about interfaces and interfaces are just a contract that says that any class that implements an interface must define the functions that are inside of the interface and this is a way for us to take advantage of polymorphism so we're going to do here is define an interface by going in typing interface and let's say we want to say singable we want to give our animals the ability to sing and we're going to come in here and find the function but we don't say what the function does we just leave it like that and what that means is when we go into say our dog class and say implements and then singable that it is going to force us to define sing inside of it so we're going to say function sing and if you don't define anything a function is automatically public and we're gonna say echo this name sings then we'll do something like or gegege semicolon at the end and then we're going to do exactly the same thing with our animal class so come up here and the great thing about using interfaces is you can only ever extend one object but you can implement an rending array of interfaces so that's why they are very useful so singable it's going to say we have to create the sync class insider here or the sing method there we go and there we are and then we're just bow-wow-wow then if we come down here and go animal 1 or animal 2 or whatever and call sing it is going to print that out on our screen so there's an example using an interface another example of polymorphism is here where we're going to be able to define functions that are going to be able to accept classes that extend a specific class or a specific interface here's polymorphism in action again so this is outside of any of the classes we're going to come in here and we're going to type in function and then we could say something like make them sing and then we could say singable because both the animal and the dog objects are both of type singable and you can just type in singing animal here and then we could just type in singing animal and call sang and it's automatically going to execute the right sing in the right animal even though it doesn't say here that it's a animal type or of dog type it says it's off singable type so we'll be able to just come in here and go make them sing and pass in animal 1 or animal 2 and when we execute it's going to automatically have the right animal st. say there you go and of course since you can do this with the different classes that implement the different interfaces of course you're also going to be able to do it with a dog class because it extends the animal class so here what we're going to do is go function sing animal you can see there it's animal type singing animal once again this is outside of any of the classes and I'm just going to copy this up here so I'm gonna type it out again and once again I'm gonna be able to go sing animal sing animal I know let's just copy these there you go execute and you can see it prints out the right things even though all I cited here was that I was passing animal objects okay polymorphism in action another type of function you're going to have inside of PHP is what are called static functions now static functions or methods can be called without the need to instantiate or create an object sounds like a big word but they're not really that's what it means when somebody says instantiate that means they just created an object let's just go into the animal class again and I'm going to type in static beginning this belongs to the class add these and let's say that I wanted to accept number one and a number two as like a utility function that doesn't really make sense that my animals will be able to add but I wanted to have this in here for some bizarro reason this is terrible form by the way but I'm just doing this just to show you what a static function is number one plus number two through a break statement in there and now what I can do is come down inside of here and do something like echo three plus five is equal to and if I want to call that static function I just go animal which is where it resides and then I call add these and then let's say I pass in three and five and then if I execute it you can see three plus five is equal to eight that's a static function and now run through a bunch of other different things you can do with PHP you can check the class type so let's say something like is it an animal we want to check if it's an animal you would go and put something like animal two and then instance all and we'll say animal and yes indeed it'll - even though it's a dog object is an instance of the animal object type and then we'll say that we want to assign true if it is or false if it isn't and you could go in here and put other different things inside of here and check the class type on your own echo do something like it is that animal I'm using single quotes here so I can actually type in animal two in this situation with the dollar sign is an animal very statement it is true that animal two is an animal say it printed right there another thing we could do is clone which is make a clone of an object and you could actually inside of your class define the things that you want to be copied over to any clones and you would just create a function inside of it with the magic to underscores with clone after it and then that would be called anytime something was cloned but just to keep this nice and short I'm just going to show you how to create a clone so let's go animal clone is equal to and then you would just type clone and then you'd go animal one now we just create a copy of it another thing you can do is define what are called abstract classes and also abstract methods now an abstract class and not be instantiated it cannot have an object be creative type whatever the abstract class is but instead it forces classes that implement it to override every abstract method that exists inside of it you want to create an abstract class you just type in abstract class and then whatever you want it to be called and then inside of it you're going to define all of the different methods or functions that must be used by any class that implements this abstract class so it's just random function and it could have an attribute in it or it could have whatever so there's an abstract class and then you could also use something that I almost never used which is call inside of a class and if it is defined inside of a class what this is going to do is provide method overloading but since PHP isn't strongly typed there's really no reason to use it now I just wanted to briefly give you an idea I mean I covered pretty much everything but I wanted to give you an idea of how this is used and how this is used in things like well I'm going to show you this this is very simple let's say I wanted to create a way to very quickly be able to or very easily be able to define the title description keywords and author for my website and also provide ways to get those titles and also provide ways to create the opening part of my HTML document and like I said I'm dumbing this down a lot ok so I would go in and define all these functions and all this information I then inside my website could just do this I would say require and site info dot Inc is the name of this file right here I could just come in here and very quickly whip out a site ok now things normally aren't done that way because you're normally pulling data from databases and setting things up right so what I decided to do is go into WordPress and I did a ton of tutorials and this is how WordPress and every customer management system works this is it so you can see here I'm including data that I want to use or functions I want to execute and you can see right here I have things like git header this is WordPress and this is automatically going to pull that information from this file and you can see right here that this is going to call a function and it's going to do all kinds of customized things using PHP right inside of your web page okay so that is just a rundown of object-oriented programming in general I covered this stuff in my wordpress tutorials like crazy so if you want to see more examples of how this is set up I have WordPress tutorials that cover it in extreme detail about 80 videos so all of the code is available in the description please leave your questions and comments below otherwise till next time
Info
Channel: Derek Banas
Views: 317,501
Rating: undefined out of 5
Keywords: Object Oriented PHP, PHP Programming, OOP PHP, Object Oriented Programming PHP, PHP Video Tutorial, Learn PHP, PHP (Programming Language), Object-oriented Programming (Programming Language Paradigm)
Id: 5YaF8xTmxs4
Channel Id: undefined
Length: 26min 37sec (1597 seconds)
Published: Mon Aug 18 2014
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.