Okay, so now let's learn about the static properties and methods.
Those would be properties and methods that actually belong to the class, not any particular instance.
So I'd like to remind you the analogy that we've used in the first video about classes.
So a class is Tesla model Y like a general concept when you talk about Tesla in general, if you talk
about Tesla, then you don't mean my Tesla or anyone else's Tesla, you just mean the model in general.
Maybe this can be BMW three series.
As another example, you don't talk about your uncle's BMW, but general about the car.
You get the point.
And sometimes you can also have some properties and some methods that belong to this generic concept
to a class, not particular instance of it.
And as an example, we are going to create a class that's called math utils.
And what this one will do is it is a generic, let's call it container for mathematical related utilities.
So for example, I might have a static float field which is initialized using a number which we know
is constant.
So it won't change.
And it's just handy to be able to get it everywhere.
And another thing what's handy about that is we can, for example, add many mathematical methods that
we often use, and then we just have this one place where we know everything is placed.
And that's our math utils class.
Now it's totally fine to mix static and non-static fields and methods in a class.
So this is just a specific example that we are having here.
Let me also add a method which can be called square.
So this just accepts a number.
And well this method is not really specific to any instance.
It's just like a function grouped under a class.
So this would be just number times number.
And now let's see how can we actually call those static methods and get access to static fields.
So let's vardump the results.
So we've got the p number.
So I get access to the p number field by using the class Last name double colon.
And then I access the field by prepending the name with a variable.
So this is how can I vardump the value of this p field?
And to call the method the same, I use the class name double colon and I can call the square method.
Let's say four four.
Now finally let's run this and we've got results for both.
Now let me show you another example where static methods make a lot of sense.
That's for example a singleton design pattern.
So in short singleton means that certain class should be created only once and you should not be able
to create it again.
And this is important if you have, for example, expensive resources like you need to create a database
connection.
And you want to make sure that only one connection is created and that it is then reused.
So let's create a connection class and to apply the singleton pattern, what you can do is you will
make the constructor private.
So this is a known PHP technique.
By making constructor private no one can actually create a class.
So that's connection I do new connection and I expect an error in here.
So let's run this example.
And we are calling a private constructor.
So it throws a fatal error.
So we made this constructor private.
So as I said no one can create this class explicitly.
But we can give another way to do that by adding a public static method.
It can be called singleton, or often it's also called factory.
Missing the function keyword here.
Now, since the constructor is private, it means it cannot be called from outside this class, which
means the class cannot be created using the new operator.
But I can still call this function from within the class.
So here I can do return new connection.
But this is not a singleton yet.
So I would have to add a field here.
So I would have to add private static instance.
And initialize it with null.
So by making this private and static I just make sure it's not accessible People outside of the class.
And I can do.
If null equals.
Connection instance.
So I check if the static field is null.
I initialize it.
So I do connection.
Instance equals new connection.
Let me add square brackets in here.
And then I always return the connection instance.
So how it works is I always check if the instance one and only instance of the connection class was
created already.
If it wasn't, I create it.
If it was, I just return it.
This makes sure that there will always be at most one instance of the connection class.
Now to create this connection you run the connection singleton.
Okay, let's comment this out and just run this file to make sure that there is no issue.
Okay.
So everything went fine and we actually created a class.
Now we can also make an improvement here.
So instead of directly relying on the class name inside the class we can use a static keyword.
So this is professionally called late static binding.
And what this does is essentially if you would extend the connection class then the code will just work
for the child class as well, because here if we explicitly rely on the connection class, we will always
be using all the methods and fields as they were defined in the connection class.
So this really makes this class less extendable.
And if you remember the lecture about inheritance, it's quite possible literally almost the essence
of object oriented programming to be able to extend the classes and change the functionality.
So unless you have strong reasons for it, use the static keyword, not the actual class name inside
the class definition baths because outside you need to use the concrete class name.
Otherwise PHP would not know which static method you actually want to call.