Thứ Bảy, 28 tháng 10, 2017

Auto news on Youtube Oct 28 2017

recently I have got a lot of requests to create a video tutorials based about

object-oriented programming so I have decided to create a three part video

series where I'm going to teach you about basics intermediate and advanced

settings based about object-oriented programming so in this first three that

I'm going to talk about the concept of object-oriented programming I will show

how to declare new class helped to create setters and getters what are the

properties and etc so let's do it

hey what's up guys Senaid here from codingpassiveincome.com the place where I help

others to become a web developer much easy and fast and then they will do it

on their own so if that is something that interests you consider subscribing

so now in order to explain the concept of object-oriented programming

let's imagine that you want to create our human population that we have on

this planet so as you can see we have over seven billion people in this world

and the problem is that if we want to do it from the coding side it will be

really challenging because we will need to create the same thing seven billion

plus times so now if we think about this and look each human we can notice some

patterns almost 99% of us have the same functionality the same visual appearance

so let's say that all of us have a head we have eyes we have nose mouth neck

arms somebody 2 legs etc we also have some here and really it's not important

so now if we think as a not only web developers but programmers in general we

will be smart if we create this as something abstract and then we just

multiply this a lot of the times but of course there are some things that are

different between each of us so some of those things are first-name lastname

maybe skin colour the hate debate etc and now with this object-oriented

programming what we are going to do we will just create something that is

almost the same for all of us and then to this class we will just give an

options to modify this class and then on that we create and new objects that are

basically everything the same with just the small difference so let's

say now if we have this class and we want to create a new person we will just

create it very simple we will just create create me new instance of this

class and we will get a new object which in our case of course is a new human ok

so this one will look like this and now all we need to do is just to apply and

call some methods which a procedural programming is function to just create

the difference between those two and now of course we will dust in a few lines of

the code create a lot of those so I know that this at the moment is very hard to

imagine but just remember that with the object-oriented programming what you are

doing we are actually creating one class that will have learning the same and

then in order to create a new object or instance of the this same class we will

just use one or two lines of the code and the calling the methods we will make

difference between each of them so as you can see now the object-oriented

programming is probably at the beginning fun and then you will find it very

frustrating and challenging and later you'll find it will be very cool so with

that being said please stay with me through all those steps and I'm sure

that one day we'll be happy that you took these steps so let me now show you

how to write the code and actually create some classes so now as I

understand the basic concept of object-oriented programming

let's create some class and set some setters and getters to this class so in

order to create some class we need to use the keyword class and after that we

need to give a name to this class so let's say it I will name it the person

and then we just need to open and close the bracket so now after this as you can

see this class doesn't have any method inside it so we can create some

and now before we even create any I want to show how to create a new object from

this class so let's say it will have a name maybe I better say Smith equal and

I will just say new person ok and now let's just try to execute this

I will hit refresh and there is nothing which means that our code is completely

defined but let's say that I'm going to create something that we are calling

construct ok and now you may ask so what is this function construct basically it

is a function that is called every time that we use the keyword new so if we do

not create one construct we won't call any but basically by default it will be

cold if it exists okay so let's try to do this and I will say echo new object

has been great okay and now let's try to refresh and now as you can see inside

our construct class we have got a response ok as you can see I didn't call

any method but just by using the word new this has been cold so let's say that

we will have one variable here so I will maybe say name and by default I will

just say that the name will be used ok and now inside this construct we can

pass some name so let's say name ok and in order for me to change this name

I will need to use the word this which means that we are referencing to the

current object and then I will say name = new name ok and now by default I

can say that this is an empty so I will say here F name is not empty we will

change the name of this person and then let's say I will create a new function

that we usually call getters sows a public function get name ok and I will

just say echo your name is and then he I will does say again this name ok and now

when I create a new person I will say he Senaid->getName okay so let's try to

refresh and now it says your name is user and the reason for that is because

of the construction we didn't specify any name so if I wanted to say Sanae I

was specified here and now if I refresh you can see that I will get your name is

Senaid and now as you can see at the moment we do not have any option to

change this name instead of at the time of the construction of the class so it

will be great to have a function that we call a setter which means that you are

able to change something inside this class souse a public function set name

and then I will specify name and say this->name = name okay so now

if we try to use it this way I will hit refresh and since your name

is user okay and here I will say Senaid set name so I will see Senaid so maybe

it will be better if instead of Senaid we use person okay so the name will be

Senaid and then again I want to say get name okay so let's see what we will have

refresh so as you can see it says your name is user and again it says your name

is Sydney which means that by doing this we'll have called the setters with the

name set name and then we have updated this variable okay so now if anytime you

want to get or to just change something you will always have to create setters

and getters now you may ask what if I try to do this so let's say I will touch

the like this and I will say echo person what do you think it will happen now if

i refresh now it says that object of class person could not be converted to

string which means that we are not able to just say echo person but we can do

something like this create a new function to strain which means that now

we are able to call it and to create some string so I can

say that we will return your name is by this okay and now if we refresh you can

see now that it is working but since we have he also get name and basically this

is the same we can change this to the return and here we will does say this

get name okay so what we are doing here basically from the method to string we

are calling the method get name and then this get name will return this message

so let's try refresh and it is the same result and of course if we want to

specify some name we can do it and this will be changed so as you can see now

this is where the real user is we can have different functions and each of

these function will do something really awesome at the moment it's really not

doing anything special but the understanding of those basics is

extremely important so let's now try to improve it even more so let's say that

this get name which by default not by default but this is the best practice is

that when you have the name of the function it should return only that so

if we say here get name then this function should return only the name and

not like we had in the previous example other characters so here now let's say

that we want to also set create a setter and getter for the last name

maybe I'll just duplicate this and I will just say said last name so say he

was named okay and now the same he'll get last name

okay and at the top i will also define variable last name and for the last name

by default it will be empty and now on the construct i will set that we are

also able to provide first and last name okay i will duplicate this

my name okay so now when we create some person we are able to specify the first

name and also the last name and also we have setters and getters for this person

and as you can see now the function to string by default is returning only the

get name so maybe we should improve itself declare a new function and say

identify and then I will say return my name is and then I want to say this name

and this last name we could also here called methods for this but there isn't

any reason for that and then here in the third string I will just say this

identify okay of course we can add some other functions and properties like age

skin color etc but for now let's use it just this way okay and now as you can

see here we have person John and then let's specify some last name let's say B

okay that's person number one okay and then we also want to have person number

two so I will say person and this one will not have any name or last name I

want to set for that person using methods so I'll say person to say

name and I will say his name and then for the person - I've also said last

name I'll say as signals okay so those are just two persons and now let's try

to identify each of these persons I'll say echo person one so let's see what

the result will be so I will fresh and as you can say see it says my name is B

so let's see why is that

Sunday the program is here we made small mistake okay let's try again so for the

first person says my name is don't be okay and now let's say they'll just make

one new light and say echo person to identify so as you can see now I'm

directly calling the method identified instead of doing it this way so let's

try and as you can see now it says my name is Tom B and for this one it says

mine is Bessie note so let's see what is the problem with that okay here should

change this okay and let's try again okay and now as you can see we have

created a functionality where we are able to create two different persons and

now for each person we are able to manipulate in our case the name and last

name and we are able to get those informations on two different ways and

now as you can see this code is completely the same for both persons so

now in order to make our code a lot more readable you just can cut this I will

create a new file so let's say we will name it person dot class.PHP okay

and of course you can specify this dot class it's not a must but it's just a

good practice so that we know that this file contains some class okay and now we

will just include it here so I will say include person dot class.PHP and now

if we refresh the result will be the same

and now our code is really readable we don't have a lot of the functions and we

didn't write all of this code two times we made it only the ones so maybe at the

moment you are not able to see the real use it and how much powerful

object-oriented approach is so for that reason in the next two videos I'm going

to talk about intermediate and advanced it object-oriented programming so guys I

hope that you like this video if you do please like it and share with your friends

and also if you have any questions please ask in the comments below

take care

Không có nhận xét nào:

Đăng nhận xét