What Is a Class in PHP? Instantiate Your First Object

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.

Example of a class name.

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.

Code example of a class named User with two public properties the strings $name and $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.

A public login function.

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:

Instantiation of $user1.

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.

Code example of accessing public properties with an arrow function.

We can create as many users as we want.

For example:

Code example of instantiating many users from the same class.

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.

Code example of the __construct function.

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:

Code example of passing property values through a constructor.

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:

Code example of instantiating two new users and passing values to the constructor.
Instead of manually setting the properties and accessing them, we just pass the values in the constructor.

Let’s see what we get in the browser.

Code:

var_dump the two newly created users.

Result:

Image of what var_dump shows on the browser.

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:

Code example of checking object instances with instanceof keyword.
Here, $obj object is an instance of both MyClass and AnotherClass trough inheritance. We verify it with instanceof keyword which checks if an object is an instance of a class.
Note: If the variable being tested is not an object, instanceof will return false without 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.

Although my blog doesn’t support comments, feel free to reply via email or X.