Classes and Objects in PHP - GeeksforGeeks

Classes and Objects in PHP

Last Updated : 4 Nov, 2026

In PHP, classes and objects are fundamental concepts of Object-Oriented Programming (OOP). A class defines properties and methods, while an object is an instance of that class.

Classes

A class in PHP is a blueprint for creating objects. It groups related properties (data) and methods (functions) into a single structure. A class is declared using the class keyword, followed by its name and a pair of curly braces.

php_class_n_objects

Example:

PHP
<?php
class Car {
    public $brand;
    public $model;
    public $year;

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

    public function displayInfo() {
        echo "$this->brand $this->model, $this->year";
    }
}

$car = new Car("Toyota", "Corolla", 2021);
$car->displayInfo();
?>
  • Car defines three properties: $brand, $model, and $year.
  • __construct() initializes these properties when an object is created.
  • displayInfo() displays the car's details.
  • new Car() creates an object and passes values to the constructor.

Syntax:

<?phpclass ClassName {    // Properties and methods}?>

Objects

An object is an instance of a class. It has access to the properties and methods defined by the class, subject to their visibility. PHP uses the new keyword to create an object. The -> operator accesses an object's properties and methods.

php_class_n_object2

Example:

PHP
<?php
class Car {
    public $brand;

    public function __construct($brand) {
        $this->brand = $brand;
    }

    public function displayInfo() {
        echo "Car brand: " . $this->brand;
    }
}

$myCar = new Car("Toyota");
$myCar->displayInfo();
?>
  • $myCar stores an object created from the Car class.
  • The constructor initializes the $brand property with "Toyota".
  • $myCar->displayInfo() calls the object's method.

Syntax:

$objectName = new ClassName();

$this Keyword to Access Object Members

The $this keyword refers to the current object inside a non-static method. It allows a method to access the object's properties and other methods.

PHP
<?php
class Car {
    public $brand;
    public $model;

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

    public function displayInfo() {
        echo "Car Brand: " . $this->brand .
             ", Model: " . $this->model;
    }
}

$myCar = new Car("Toyota", "Corolla");
$myCar->displayInfo();
?>
  • $this->brand accesses the current object's brand property.
  • $this->model accesses the current object's model property.
  • $this allows the same class methods to work with different objects and their respective property values.

Benefits of Using Classes and Objects in PHP

  • Code Reusability: Define properties and methods once and use them to create multiple objects.
  • Maintainability: Group related data and functionality into classes, making code easier to organize and update.
  • Extensibility: Use inheritance to extend existing classes and polymorphism to support different implementations of common interfaces or methods.
Comment