In this post, you are going to learn what a class is and how to use one to instantiate a new object.
To create a class, you use the class keyword and the class name, which by convention uses PascalCase if there is more than one word.
Inside that class, you can use properties.
Let’s see an example.
We have a class named User, and we are going to define a name and an email.
So from this class we can instantiate an object, and this object can have these two properties.
The public keyword here is used to identify the accessibility of the property. Public means that the property can be accessed from outside of the class.
Adding Methods
Apart from properties, we can create methods in our classes. A method is just a function that belongs to a class.
We create a method as we do with a regular function, but we also use an identifier as we do with the properties.
Let’s create a function called login as an example. This is of course a dummy login function since creating a proper login system would be to much for this post.
How to instantiate a new object from a class
A class is literally an object creation blueprint. We can use a class as a template for creating as many objects as we want. All these objects inherit the properties and methods of the class.
So we are going to use the User class in our example to create some new users.
To create, or instantiate, which is the official term, a new object, we need to use the new keyword:
Now, since the properties and the method in the class are public we can access them outside of the class.
We use the arrow syntax to do that.
We can create as many users as we want.
For example:
But this is not the most efficient way to assign property values to our newly instantiated users.
Firstly, because it’s not practical (too messy code), and secondly because most of the time these properties won’t be accessible outside of the class.
Using the Constructor Method
An alternate, more efficient, and popular way to do this is to use a constructor, also known as the construct method.
This is a specifically named method created in the class using the syntax of a typical function but with the special name __construct. This method runs whenever a new object is instantiated.
That is when we make use of the new keyword.
So it is easier to pass property values through a constructor.
We assign the passed-in parameters to the class properties.
And we set the name and email of each object to whatever name and email are passed into the constructor.
For example:
The $this→name in this constructor pertains to whichever user we are looking at.
We can access the class properties form any method, the constructor included, by using the $this keyword. The $this keyword just means “this particular object“.
So now we just pass the arguments to the constructor:
Let’s see what we get in the browser.
Code:
Result:

Checking Object Instances in PHP
In PHP, you can check if an object is an instance of a specific class using the instanceof keyword, which returns true if the object is an instance of that class or inherits from it.
Here’s a simple example demonstrating how to use instanceof:
Note: If the variable being tested is not an object,instanceofwill returnfalsewithout throwing an error.
Conclusion
In PHP, a class is a blueprint for creating objects, which are instances of that class. To instantiate an object from a class, you use the new keyword, followed by the class name.
Using classes and objects in PHP development offers several advantages, including improved code organization and reusability, which make it easier to maintain and modify code.