Okay.
It's finally the time that we learn about the objects and classes.
First, let me use a metaphor to explain objects and classes.
So class is like a boiler plate of a car.
Let's say it is a boiler plate on.
How do you build Tesla model Y?
So you can think of this as a generic concept.
When you are talking about a Tesla model Y, you don't mean my Tesla or your Tesla.
You generally mean the car.
But then an object is basically an instance of this Tesla model Y.
And when we are talking about objects, I'm talking about my Tesla, which can have its own mileage
or its own acceleration.
And also we might be talking about your Tesla, which might have a different color, different mileage,
etc..
So that's the main difference between objects and classes.
Now you just define this boilerplate by using classes and then create objects based on those boilerplates.
Now let's create a boilerplate which is a class, and let's make it a person.
Now the classes can have data or methods.
Data is basically class variables and methods are class functions.
So in case of person we can have a string name.
And also another property as it's also called can be age.
Now we know that every person has a name and every person has an age.
But once we create an object from this class, we are talking about my name, which is Pyotr, and my
age, which currently is 37.
So this is a field That's the type of that field.
And this is the visibility keyword.
Let's make everything public for now.
And I'm gonna go back to this later on.
Next up we can have methods.
So methods are basically functions which also have this visibility keyword.
So let's make an introduce method which would return a string.
And this would be something pretty simple.
It's just gonna say hi but we're going to use this data this field that we've defined above.
So the main difference between using just functions and classes and objects is that when you have an
object, you don't really have to pass around the data to all the functions, because those objects
will just have its own data, like its own name and own age.
And to access those, we're going to use a special variable called this and it refers to this current
object.
And we use the arrow operator to get the name.
And I can also say that I am.
Specific age years old.
Okay.
So that was a class definition.
Now let's create an object out of this.
So I can have a person variable.
And I can make a new person using the new keyword, after which goes the class name.
Next up I can access the public fields and public methods of this created object, because person variable
is now an object, an instance of the class using the arrow operator.
So I can call the introduce method.
So let's see what would be the output.
Well we actually got a fatal error that we are accessing a typed property that wasn't really initialized.
Well, actually, that's the case.
We haven't initialized those properties and we try to read their value.
So let's see what's the typical way in classes to initialize values?
Well, every single class can have a special method that is called constructor.
Now constructor is just a function but it has a special name it's underscore underscore construct.
And this will be always called whenever you create a new object using the new operator.
It's just called automatically implicitly.
And this constructor can accept some parameters like name and age.
And we can use this to initialize the data of this object.
So this name will equal name and this age will equal age.
Now when we create the objects we can pass parameters.
And the parameters are accepted here are same as in the constructor.
So there should be a name I can do a lease.
And let's say that Alice is 30.
And when I run this now we've got this message.
I am Alice and I am 30 years old.
Now let me create a person.
Number two.
And this would be me this time so that we can see the difference.
That this object has completely different data inside.
So this is for Alice and this is for me.
So the way this constructor is written where you pass something to the class and this is initializing
the object data is something super common.
One of the most common things that you're gonna do, you just need to pass some initial data to the
created object.
Now this was so common that in PHP eight a constructor property promotion was introduced.
I spelled that slowly because this is how it is called professionally.
And thanks to constructor property promotion, you don't really have to do this data initialization
in a constructor, which can be cumbersome and lazy.
People don't like cumbersome, daunting tasks.
Now I can remove that and to promote this to properties, I can just add visibility keywords next to
the constructor arguments.
And then I also don't need to define or I can't define those properties right here.
Now having those constructor property promotion is essentially the same as having the properties defined
and initializing them explicitly in a constructor.
So in cases where you don't really need to do anything extra with the past data, because this is why
you can still pass the arguments through constructor and assign them to the data to the fields.
You just might need to do something extra, but if you don't, then that's exactly the same thing and
it's more concise.
If we run this code again, it still works.