How to declare a class in PHP

Updated: January 11, 2024 By: Guest Contributor Post a comment

Understanding the Basics

In the world of PHP, classes are the blueprints for creating objects which are instances of those classes. Learning how to declare a class is a fundamental skill for any PHP developer looking to leverage the power of object-oriented programming (OOP) to create more reusabnle, scalable, and organized code. In this tutorial, we’ll cover everything you need to know about declaring classes in PHP, including properties, methods, and more.

Before we jump into declaring classes, it’s important to understand the basics of OOP. A class in OOP parlance, is a template or blueprint for creating objects. An object is an instance of a class. Classes encapsulate data for the object and methods to manipulate that data.

Define a Simple Class

class MyFirstClass {
   // Properties and methods go here
} 

Using the class keyword followed by the name of the class and curly braces, you can declare a class in PHP. It’s best practice to start the name of your class with a capital letter.

Adding Properties

Properties are variables within a class that are used to hold values specific to the object.

class Car {
   public $model;
   public $color;
}

In this example, we added two properties ($model and $color) to our Car class. Properties should be declared with a visibility keyword: public, protected, or private to define their access level.

Declaring Methods

Methods are functions that belong to a class. Just like properties, methods can also have visibility keywords, and immediately following the keyword, you should add the function keyword and the method’s name.

class Car {
   public $model;
   public $color;

   public function getCarModel() {
     return $this->model;
  }
}

Here we’ve added a method named getCarModel that would return the model of the car.

Constructors

Constructors are special methods that are automatically called when an object is created. They often serve to initialize the object’s properties or perform startup procedures.

class Car {
   public $model;
   public $color;

   public function __construct($model, $color) {
     $this->model = $model;
     $this->color = $color;
   }
}

We have now added a constructor method that takes two parameters and assigns them to the object’s properties.

Inheritance

Inheritance is a concept that allows one class to inherit the properties and methods of another. We use the extends keyword to achieve this:

class ElectricCar extends Car {
   public $batteryRange;

   public function getBatteryRange() {
      return $this->batteryRange;
   }
}

ElectricCar now has all the properties and methods of Car, plus its own additional property and method.

Final Thoughts

Declaring classes is just the beginning. PHP 7 and onwards offer features like type declarations, property typing, and more advanced OOP concepts that you can use to enhance your applications. Remember that declaring classes in a coherent, sensible manner can drastically improve your code organization and reuse—and it’s a big step towards mastering PHP development.