WEBVTT

00:00.710 --> 00:01.220
Okay.

00:01.220 --> 00:05.720
It's finally the time that we learn about the objects and classes.

00:05.870 --> 00:10.430
First, let me use a metaphor to explain objects and classes.

00:10.580 --> 00:13.850
So class is like a boiler plate of a car.

00:13.880 --> 00:16.580
Let's say it is a boiler plate on.

00:16.580 --> 00:19.130
How do you build Tesla model Y?

00:19.160 --> 00:22.280
So you can think of this as a generic concept.

00:22.280 --> 00:28.910
When you are talking about a Tesla model Y, you don't mean my Tesla or your Tesla.

00:28.940 --> 00:31.040
You generally mean the car.

00:31.070 --> 00:37.820
But then an object is basically an instance of this Tesla model Y.

00:37.820 --> 00:44.150
And when we are talking about objects, I'm talking about my Tesla, which can have its own mileage

00:44.180 --> 00:46.820
or its own acceleration.

00:47.090 --> 00:54.740
And also we might be talking about your Tesla, which might have a different color, different mileage,

00:54.740 --> 00:55.730
etc..

00:55.730 --> 00:59.540
So that's the main difference between objects and classes.

00:59.570 --> 01:09.550
Now you just define this boilerplate by using classes and then create objects based on those boilerplates.

01:09.580 --> 01:17.020
Now let's create a boilerplate which is a class, and let's make it a person.

01:17.050 --> 01:21.460
Now the classes can have data or methods.

01:21.460 --> 01:27.550
Data is basically class variables and methods are class functions.

01:27.550 --> 01:33.610
So in case of person we can have a string name.

01:34.630 --> 01:41.050
And also another property as it's also called can be age.

01:41.320 --> 01:47.920
Now we know that every person has a name and every person has an age.

01:47.920 --> 01:54.400
But once we create an object from this class, we are talking about my name, which is Pyotr, and my

01:54.430 --> 01:57.250
age, which currently is 37.

01:58.420 --> 02:02.010
So this is a field That's the type of that field.

02:02.010 --> 02:04.920
And this is the visibility keyword.

02:04.920 --> 02:07.050
Let's make everything public for now.

02:07.050 --> 02:10.200
And I'm gonna go back to this later on.

02:11.130 --> 02:13.140
Next up we can have methods.

02:13.140 --> 02:18.960
So methods are basically functions which also have this visibility keyword.

02:19.110 --> 02:24.780
So let's make an introduce method which would return a string.

02:25.170 --> 02:28.500
And this would be something pretty simple.

02:28.500 --> 02:36.180
It's just gonna say hi but we're going to use this data this field that we've defined above.

02:36.180 --> 02:44.370
So the main difference between using just functions and classes and objects is that when you have an

02:44.370 --> 02:50.820
object, you don't really have to pass around the data to all the functions, because those objects

02:50.820 --> 02:55.620
will just have its own data, like its own name and own age.

02:56.070 --> 03:02.600
And to access those, we're going to use a special variable called this and it refers to this current

03:02.630 --> 03:03.650
object.

03:03.650 --> 03:07.790
And we use the arrow operator to get the name.

03:07.880 --> 03:10.760
And I can also say that I am.

03:13.160 --> 03:16.820
Specific age years old.

03:17.300 --> 03:17.600
Okay.

03:17.600 --> 03:19.460
So that was a class definition.

03:19.460 --> 03:21.950
Now let's create an object out of this.

03:21.950 --> 03:24.140
So I can have a person variable.

03:24.140 --> 03:29.600
And I can make a new person using the new keyword, after which goes the class name.

03:30.470 --> 03:39.650
Next up I can access the public fields and public methods of this created object, because person variable

03:39.650 --> 03:45.110
is now an object, an instance of the class using the arrow operator.

03:45.410 --> 03:49.070
So I can call the introduce method.

03:49.910 --> 03:53.660
So let's see what would be the output.

03:54.320 --> 04:03.850
Well we actually got a fatal error that we are accessing a typed property that wasn't really initialized.

04:05.890 --> 04:07.660
Well, actually, that's the case.

04:07.660 --> 04:12.370
We haven't initialized those properties and we try to read their value.

04:12.400 --> 04:17.440
So let's see what's the typical way in classes to initialize values?

04:17.470 --> 04:23.890
Well, every single class can have a special method that is called constructor.

04:24.520 --> 04:33.340
Now constructor is just a function but it has a special name it's underscore underscore construct.

04:34.450 --> 04:40.930
And this will be always called whenever you create a new object using the new operator.

04:40.930 --> 04:44.740
It's just called automatically implicitly.

04:44.830 --> 04:52.990
And this constructor can accept some parameters like name and age.

04:53.680 --> 04:58.660
And we can use this to initialize the data of this object.

04:58.660 --> 05:04.470
So this name will equal name and this age will equal age.

05:04.500 --> 05:07.710
Now when we create the objects we can pass parameters.

05:07.710 --> 05:12.960
And the parameters are accepted here are same as in the constructor.

05:13.440 --> 05:17.460
So there should be a name I can do a lease.

05:18.210 --> 05:21.300
And let's say that Alice is 30.

05:21.300 --> 05:25.590
And when I run this now we've got this message.

05:25.590 --> 05:28.920
I am Alice and I am 30 years old.

05:30.120 --> 05:32.580
Now let me create a person.

05:32.580 --> 05:34.020
Number two.

05:34.050 --> 05:43.020
And this would be me this time so that we can see the difference.

05:45.870 --> 05:51.210
That this object has completely different data inside.

05:53.520 --> 05:56.820
So this is for Alice and this is for me.

05:58.670 --> 06:07.460
So the way this constructor is written where you pass something to the class and this is initializing

06:07.460 --> 06:11.780
the object data is something super common.

06:11.780 --> 06:18.800
One of the most common things that you're gonna do, you just need to pass some initial data to the

06:18.800 --> 06:20.120
created object.

06:20.150 --> 06:29.690
Now this was so common that in PHP eight a constructor property promotion was introduced.

06:29.690 --> 06:35.000
I spelled that slowly because this is how it is called professionally.

06:35.210 --> 06:43.700
And thanks to constructor property promotion, you don't really have to do this data initialization

06:43.700 --> 06:48.890
in a constructor, which can be cumbersome and lazy.

06:48.890 --> 06:53.060
People don't like cumbersome, daunting tasks.

06:53.450 --> 07:02.200
Now I can remove that and to promote this to properties, I can just add visibility keywords next to

07:02.230 --> 07:04.840
the constructor arguments.

07:04.840 --> 07:12.280
And then I also don't need to define or I can't define those properties right here.

07:12.430 --> 07:21.760
Now having those constructor property promotion is essentially the same as having the properties defined

07:21.760 --> 07:26.140
and initializing them explicitly in a constructor.

07:26.260 --> 07:34.690
So in cases where you don't really need to do anything extra with the past data, because this is why

07:34.690 --> 07:43.060
you can still pass the arguments through constructor and assign them to the data to the fields.

07:43.120 --> 07:52.180
You just might need to do something extra, but if you don't, then that's exactly the same thing and

07:52.180 --> 07:53.860
it's more concise.

07:54.130 --> 07:57.820
If we run this code again, it still works.
