WEBVTT

00:00.200 --> 00:04.640
Okay, so now let's learn about the static properties and methods.

00:04.640 --> 00:13.070
Those would be properties and methods that actually belong to the class, not any particular instance.

00:13.160 --> 00:20.120
So I'd like to remind you the analogy that we've used in the first video about classes.

00:20.270 --> 00:29.390
So a class is Tesla model Y like a general concept when you talk about Tesla in general, if you talk

00:29.420 --> 00:37.580
about Tesla, then you don't mean my Tesla or anyone else's Tesla, you just mean the model in general.

00:37.610 --> 00:41.930
Maybe this can be BMW three series.

00:41.960 --> 00:48.200
As another example, you don't talk about your uncle's BMW, but general about the car.

00:48.200 --> 00:49.640
You get the point.

00:50.870 --> 01:01.100
And sometimes you can also have some properties and some methods that belong to this generic concept

01:01.100 --> 01:04.260
to a class, not particular instance of it.

01:04.260 --> 01:11.580
And as an example, we are going to create a class that's called math utils.

01:11.580 --> 01:22.680
And what this one will do is it is a generic, let's call it container for mathematical related utilities.

01:24.480 --> 01:35.160
So for example, I might have a static float field which is initialized using a number which we know

01:35.160 --> 01:35.910
is constant.

01:35.910 --> 01:37.650
So it won't change.

01:37.650 --> 01:42.900
And it's just handy to be able to get it everywhere.

01:43.110 --> 01:52.380
And another thing what's handy about that is we can, for example, add many mathematical methods that

01:52.380 --> 01:59.130
we often use, and then we just have this one place where we know everything is placed.

01:59.130 --> 02:02.410
And that's our math utils class.

02:02.410 --> 02:10.060
Now it's totally fine to mix static and non-static fields and methods in a class.

02:10.060 --> 02:14.170
So this is just a specific example that we are having here.

02:14.440 --> 02:21.490
Let me also add a method which can be called square.

02:21.700 --> 02:23.860
So this just accepts a number.

02:23.860 --> 02:28.630
And well this method is not really specific to any instance.

02:28.630 --> 02:33.220
It's just like a function grouped under a class.

02:33.430 --> 02:39.520
So this would be just number times number.

02:40.180 --> 02:48.700
And now let's see how can we actually call those static methods and get access to static fields.

02:50.230 --> 02:53.440
So let's vardump the results.

02:53.440 --> 02:56.410
So we've got the p number.

02:57.490 --> 03:03.440
So I get access to the p number field by using the class Last name double colon.

03:03.440 --> 03:09.110
And then I access the field by prepending the name with a variable.

03:10.520 --> 03:14.690
So this is how can I vardump the value of this p field?

03:14.690 --> 03:21.590
And to call the method the same, I use the class name double colon and I can call the square method.

03:21.590 --> 03:24.380
Let's say four four.

03:26.510 --> 03:32.510
Now finally let's run this and we've got results for both.

03:33.440 --> 03:39.260
Now let me show you another example where static methods make a lot of sense.

03:39.290 --> 03:43.070
That's for example a singleton design pattern.

03:43.100 --> 03:51.020
So in short singleton means that certain class should be created only once and you should not be able

03:51.020 --> 03:52.340
to create it again.

03:52.340 --> 04:00.770
And this is important if you have, for example, expensive resources like you need to create a database

04:00.770 --> 04:01.430
connection.

04:01.430 --> 04:07.890
And you want to make sure that only one connection is created and that it is then reused.

04:09.810 --> 04:19.020
So let's create a connection class and to apply the singleton pattern, what you can do is you will

04:19.020 --> 04:21.810
make the constructor private.

04:22.050 --> 04:24.570
So this is a known PHP technique.

04:24.570 --> 04:30.930
By making constructor private no one can actually create a class.

04:31.110 --> 04:38.220
So that's connection I do new connection and I expect an error in here.

04:38.220 --> 04:41.040
So let's run this example.

04:41.490 --> 04:44.790
And we are calling a private constructor.

04:44.790 --> 04:47.310
So it throws a fatal error.

04:47.310 --> 04:50.670
So we made this constructor private.

04:50.670 --> 04:54.090
So as I said no one can create this class explicitly.

04:54.120 --> 05:01.470
But we can give another way to do that by adding a public static method.

05:01.860 --> 05:08.610
It can be called singleton, or often it's also called factory.

05:09.750 --> 05:12.360
Missing the function keyword here.

05:12.720 --> 05:20.580
Now, since the constructor is private, it means it cannot be called from outside this class, which

05:20.580 --> 05:24.210
means the class cannot be created using the new operator.

05:24.510 --> 05:29.910
But I can still call this function from within the class.

05:30.360 --> 05:36.240
So here I can do return new connection.

05:38.790 --> 05:41.100
But this is not a singleton yet.

05:42.240 --> 05:44.760
So I would have to add a field here.

05:45.420 --> 05:51.450
So I would have to add private static instance.

05:53.730 --> 05:55.980
And initialize it with null.

05:56.010 --> 06:04.690
So by making this private and static I just make sure it's not accessible People outside of the class.

06:07.990 --> 06:09.130
And I can do.

06:09.160 --> 06:12.430
If null equals.

06:15.790 --> 06:19.330
Connection instance.

06:20.890 --> 06:23.740
So I check if the static field is null.

06:24.820 --> 06:26.410
I initialize it.

06:27.340 --> 06:29.830
So I do connection.

06:32.590 --> 06:35.140
Instance equals new connection.

06:36.580 --> 06:38.830
Let me add square brackets in here.

06:42.880 --> 06:48.010
And then I always return the connection instance.

06:50.680 --> 06:59.050
So how it works is I always check if the instance one and only instance of the connection class was

06:59.050 --> 07:00.370
created already.

07:00.430 --> 07:02.740
If it wasn't, I create it.

07:02.770 --> 07:04.820
If it was, I just return it.

07:04.850 --> 07:13.280
This makes sure that there will always be at most one instance of the connection class.

07:13.610 --> 07:21.380
Now to create this connection you run the connection singleton.

07:24.260 --> 07:30.950
Okay, let's comment this out and just run this file to make sure that there is no issue.

07:31.250 --> 07:31.580
Okay.

07:31.610 --> 07:36.620
So everything went fine and we actually created a class.

07:37.520 --> 07:39.980
Now we can also make an improvement here.

07:39.980 --> 07:50.000
So instead of directly relying on the class name inside the class we can use a static keyword.

07:50.870 --> 07:56.810
So this is professionally called late static binding.

07:56.840 --> 08:05.760
And what this does is essentially if you would extend the connection class then the code will just work

08:05.760 --> 08:15.060
for the child class as well, because here if we explicitly rely on the connection class, we will always

08:15.090 --> 08:21.600
be using all the methods and fields as they were defined in the connection class.

08:21.600 --> 08:26.880
So this really makes this class less extendable.

08:26.940 --> 08:36.030
And if you remember the lecture about inheritance, it's quite possible literally almost the essence

08:36.030 --> 08:42.870
of object oriented programming to be able to extend the classes and change the functionality.

08:42.870 --> 08:50.880
So unless you have strong reasons for it, use the static keyword, not the actual class name inside

08:50.880 --> 08:58.110
the class definition baths because outside you need to use the concrete class name.

08:58.110 --> 09:03.720
Otherwise PHP would not know which static method you actually want to call.
