How to Create a Class in PHP

php


In this article, we go over how to create a class in PHP.

A class represents a blueprint or template of objects.

For example, if you dealing with a bunch of objects that represent animals, such as a lion, a dog, a cheetah, a pig, the class may be appropriately named animals. This is because all of the objects are animals. Thus, naming the class animals represents the temple or blueprint of these objects.

A class is the blueprint of object-oriented programming (OOP).

Before you can create objects, you must create a class that represents the template of these objects.

PHP, especially versions 5 and now, now support OOP.

To create a class in PHP, the general format to do so is shown below.



So in the above code, we have created a class named animals.

All you need to do to create a class in PHP is to use the keyword class and follow it with the class name.

Unlike Java, for instance, you do not add any keywords before class like public or private. PHP simply uses class by itself. Trying to use public or private with a class will result in a fatal error, in which the program will not run. You simply need to use the keyword class followed by the class name.

Inside of this class, we put the properties, objects, and methods that you want in this class. For example, you may have objects such as pigs, dogs, elephants, etc.

Methods may include what the animals do, such as eat, run, etc.

Properties may include features of the animals such as scaly skin, smooth skin, etc.

We show the full PHP code of how to create a class in PHP.



So in the above code, we've created a class named animals. To create a class in PHP, all you have to do is use the keyword class followed by the name of the class.

Just for demonstration purposes, we made a function in this class called the eat() function. In this function, the animal declares "I eat food", which is a characteristic of every animal on the plantet, including humans.

We then create an instance of the animals class, $lion. This represents a lion.

We then give the chief characteristic of the lion, which is that it is king of the jungle.

We then echo out the lion's characteristic, as well as the lion carrying out the eat() function, in which it declares that "I eat food".

This just goes to show you what can be done by creating a class. We can create methods within the class, objects within the class, and so on and so forth.

So creating a class is very simple. All that is needed is the PHP keyword class followed by the class name. By convention, PHP classes are normally in lowercase but doesn't have to be. It's just that that's the normal convention.

Once you create a class, you delve into the world of OOP in which you have many limitless possibilities of what you can do based on that class. Possibilities include extending the class, in which you create another class that inherits all the properties, methods, and functions of that original class. There's many things you can do in the world of OOP that applies not just to PHP, but to all OO porgramming languages including Java and C++.

Going back to the PHP code above, if it's run, this gives the following output shown below.

Actual PHP Output


Lion Characteristic: King of the jungle
I eat food


How to Create a Child Class

To create a child class of a class, you can use the keyword extends.

The format to create a child class is shown below.



This code creates a new class named dogs, which is a child class of the pets class.

This dogs class inherits every method and property of the animals class.

This is a fundamental principle of object-oriented programming called inheritance. The child class inherits everything that is in the parent class. In this case, the parent class is animals. Then you can add specialized methods and properties to that child class that may pertain to it.

As an example, we have the full code of creating a child class shown below.



So we begin by creating a class named animals.

In this animals class, we create a variable named $skin that is set equal to the value of furry.

Later when we create a child class of the animals class, you'll see that this child class will inherit this $skin variable value of furry.

We then create a child class of the animals class via using the extends keyword.

Inside of this class, we create a variable called $sounds which is set equal to barks, since a dog barks.

We then create an object of the dogs class, named $bulldog.

We then echo out the bulldog's skin, which is furry. This is because the dogs class inherits the $skin variable of furry from the parent animals class.

Inside of the dogs class itself, we create a variable named $sounds which is equal to bark. This is a specialized value to the dogs class and is unique to the dogs class. It's not found in the animals class. It's specialized to the dogs class.

We then echo out these values.

And this is how a child class can be built in PHP.

If we run the above code, this yields the following output below.

Actual PHP Output


The dog's skin: furry
A dog barks



HTML Comment Box is loading comments...