What is a PHP class?

Find out what a class is in PHP with our informative guide. Discover how classes are used in object-oriented programming, how they can help you organize your code, and how to create your own classes in PHP. Start building better PHP applications today!

As a newbie developer I have struggled a lot in the past to understand what I’m going to share with you below and I hope I can save you some time and effort by giving you the tools to harness this exceptionally useful and robust principle, the class.

A PHP class it’s a blueprint that instructs the creation of a certain type of object. Object’s characteristics are determined by its properties and methods that incorporates. Each object may differ upon creation and therefore each class can be manipulated accordingly for a variety of different instances.

Classes are used with various design patterns (such as MVC) and their main function is to describe objects and their relationships. Classes also fall under the Object Oriented Paradigm spectrum that essentially was created for non-sequential coding architectures that are maintainable, scalable and reliable by nature. In order to familiarize ourselves with the notion of the class, we first need to define certain crucial components such as what’s an instance, a constructor, class methods and properties.

What is an Instance in PHP

First things first, the instance. An instance it’s basically an object that was created using a specific blueprint (class) and may vary upon characteristics. There can be multiple instances of a single class and there may be multiple properties’ values that essentially differentiate each object.

Properties of an object

Each object’s characteristics we call them properties. Properties can be as simple as numbers e.g. width and height of door or can be some other object based on a different blueprint. All properties are accessible via that blueprint’s scope. The scope is the area of all living variables that cannot exist outside the class and are bound to each instance. Suppose you want to define a class to create humans! How would you do that? Let’s see.

class Human {
public $first, $last, $age;
};

You may be thinking “wow, that’s so easy!”. Well, this is really the simplest class definition you can think of in terms of context. As you can see the syntax is quite intuitive. You define a class name using the reserved word class and then you define a few properties. In this case, the access modifier is set to public which means that once you create an object of this class, you can access each property directly and change it.

class Human {
public $first, $last, $age;
};
$human = new Human(); // create an instance
$human->first = 'John'; // access property: first
echo $human->first; // prints: John

The class constructor and how do we use it

Let’s explain the three last lines of the previous snippet. The first of the three lines it’s what we call the instance creation using the class constructor. A class constructor is a function that can be called with the name of the class and initializes all those properties (if there are any -in this case there are three-) of the object and returns its reference. The object that’s created is the variable $human and the constructor can be spotted whenever there’s a new keyword present. Usually you are tasked to create your own constructor by passing different properties depending a given requirement.

class Person {
    public $first, $last;
    public function __construct($first, $last){
        $this->first = $first;
        $this->last = $last;
    }
    public function view(){
        echo 'Hello ' . $this->first . ' ' . $this->last;
    }
}
$karl = new Person('Karl', 'Jacob');
$karl->view(); // prints: Hello Karl Jacob

As you can see, we create an object by passing a first and a last name to the constructor function binding those values to the corresponding properties. Each property name used it’s part of a convention. The $this keyword represents a practical way to refer to this particular object of the class that was created. In this context $this it’s the same as $karl and since this variable is a class object we translate the behavior mapped within the class, hence $karl->view(). If we can sneak peek inside the class inner operations, we would only see $this->view(). Function view is nothing but a convenient method that we use to print out properties of the object in certain formats. Methods are the actions that an object can execute and it is important to keep in mind the way each of these methods can mutate (change) each property. Imagine an object derived from a class named Door. What kind of actions a door can execute? Open, Close or Slide. There you go. Now you can map three distinct methods and depending the context, change the state of your door object.