Creating classes, properties, methods - OOP in PHP | Part 1

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
class in object-oriented programming is a template blueprint you can also think as a new data type which has assaults associated variables called properties and associated functions called methods let's create our first class in PHP to create a class you need to use class keyword after class you need to name it the class name should start with either underscore or letter and then you can contain underscore later or number let me call my class car after this you use braces and here's our first class this already class it doesn't have yet associated properties and methods but we can create an instance object from this new data type and the instance is created like variable name my car is new car and that's it my car is an object instance created from this new data type called car so in the future when I say class I mean the class car and when I am talking about instance I need my car an object created from this instance from this class excuse me now let's add some properties and methods to this class like color weight and Greeley's your ok what is public and private you probably have this question public private and also protected is a visibility of properties and methods right now I'm not going to talk about protected because it's a part of inheritance which we're going to cover in the next videos but is for public and private I'm gonna explain it right now when you declare a property with public keyword you can access this property from outside the class like right here you can consider the area of class like the opening brace and closing brace so outside these braces is the out of the class so when I declare property with public keyword I can actually modify or change or get the value from it from outside the class for example I can say that my car and this arrow and the arrow is the operator which gives me access to the property or method in this case I want to access my color property so I access it and assign it whatever I want that's the tricky point I can actually assign these color whatever I want because PHP is loosely typed language and when you declare a variable and color is a variable you can actually assign it whatever you want so my cow my cars color is red and now I can print my cars color and it tells me that this is red open terminal PHP car PHP and here is red okay what about private I can only access my private properties from inside the class I cannot access it right here if I try to obsess like yar is 2010 this will give me an error and the error says cannot access private property car er okay I'm gonna delete this line but how can I access my yard property from inside the class wherever it's allowed inside functions also known as methods so in the future when I talk about function inside the class associated to this class I always mentioned as a method and it's called a method actually so I'm gonna create a new method like public function set yar which will accept an argument in here I'm gonna each we change my year how can I do this I used these keyword and this is a variable associated to the instance on which these method is called so right now I'm gonna say this yard is yard and whenever I call this set a yard method from outside the class like my car set er is 2010 this will reference to my car variable the instance on which the function the method was executed now it's allowed to change the yard and here I want to print if my yard was changed or not but I cannot use this private yard variable property yet so I'm going to create another function public function get er which will return just er of the car now with the same syntax I call get yard let me print this let me comment this color and here is 2010 which is printed on this line okay you probably noticed that I have used public keyword also when declared function I can omit these public keywords right here because by default is considered functions are considered as a public functions public methods of the class but it's good and common practice to use these visibility keywords either private public or protected on the methods to have to have a view what is the visibility actual visibility of the class you probably forget what's the default visibility so that's why it's a good practice to have these public keyword on the functions methods let's get back to one really important and tricky thing when I wrote the line 26 I said that I can assign my color whatever I want and that's quite true I can actually assign like 2010 to color which is which is I think not really good so when I print the color it's 2010 and I can do this so is there a way to restrict this and validate if we are assigning a valid color are not to the car yes there is there is a better way of assigning the color to the car and for this we're gonna use methods now I'm gonna change my color visibility to private to avoid accidental assign accidental mistake I'm assigned to the car so now when I run this I have an error that I cannot access private property of color of the car so now I'm gonna comment these two lines and I'm gonna change my code so that I can assign the color to the car via method so I'm gonna create public method set color which will accept color a give argument and here I can do the same thing these color equals to leave in color but this that doesn't make sense I don't do any validation so if I call set color it will just do the same thing so assign 2010 to the color which is not what I want so how can I do validation there is a way to create a private variable available colors and I can have a ways of valid colors like red green blue white and so on and before I assign a color to the car I can check if the color given color is barely variable LED color are not so I'm gonna right here if in array even color of these excuse me available colors if given color is in the list of available colors then I can assign my color so now if I run the code and here I need to change to get color and I need to create this public method also public function Eid color okay now when I run this my output is empty because the initial value of the color is just no so let me assign something there so let's say that the initial value is red now when I try to assign in 2010 my output tells me the color of the car is red it wasn't assigned because I passed here invalid color but if I pass here white which is in the list of available colors and I run the code I can see that my color was changed if I have a typo here like and pass it like white so this will not assign it and my color is still red so it's a really good practice to declare your variables as a private and create methods for assigning and returning values of these private properties and this is called also as encapsulation so you encapsulate your variables in methods and just work with the variables with the properties using methods that's the end of this video I hope now you understand how you can create class properties methods and instances in PHP if you liked the video please hit the like and subscribe buttons and share this video on your social media or to your friends and let me know what do you think also in the comments I'm looking forward your feedback thanks again and see you in the next time
Info
Channel: The Codeholic
Views: 10,143
Rating: undefined out of 5
Keywords: oop in php, object oriented php, object oriented programming, php oop, php tutorial, php object oriented programming tutorial for beginners, oop php introduction, php object oriented programming, introduction to oop php, object oriented programming php, introduction to oop php programming, php object oriented programming for beginners, oop php, oop php tutorial, Object-Oriented PHP tutorials, create classes in php, classes in php, PHP classes and methods, using classes in php
Id: 4xvCT7UPY3k
Channel Id: undefined
Length: 11min 2sec (662 seconds)
Published: Wed Apr 24 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.