Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Classes and Objects in PHP

Introduction to Object-Oriented Programming

Object-Oriented Programming (OOP) is a programming paradigm based on the concept of "objects". Objects are instances of classes, which can hold both data and functions. PHP, being a versatile scripting language, supports OOP, which allows developers to create modular, reusable, and maintainable code.

What is a Class?

A class is a blueprint for creating objects. It defines a set of properties (variables) and methods (functions) that the objects created from the class will have.

Example

class Car {
    public $color;
    public $model;

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

    public function getCarDetails() {
        return "This car is a " . $this->color . " " . $this->model;
    }
}
                

Creating an Object

Once a class is defined, you can create objects from it. Each object is an instance of the class and can have its own values for the properties defined in the class.

Example

$myCar = new Car("red", "Toyota");
echo $myCar->getCarDetails();
                
Output: This car is a red Toyota

Class Properties and Methods

Properties are variables that belong to a class. Methods are functions that belong to a class. You can access properties and methods of an object using the arrow operator (->).

Example

class Person {
    public $name;
    public $age;

    public function __construct($name, $age) {
        $this->name = $name;
        $this->age = $age;
    }

    public function introduce() {
        return "Hi, I'm " . $this->name . " and I'm " . $this->age . " years old.";
    }
}

$person = new Person("John", 25);
echo $person->introduce();
                
Output: Hi, I'm John and I'm 25 years old.

Access Modifiers

Access modifiers define the visibility of the properties and methods of a class. PHP has three access modifiers: public, protected, and private.

  • public: The property or method can be accessed from anywhere.
  • protected: The property or method can be accessed within the class and by classes derived from that class.
  • private: The property or method can only be accessed within the class.

Example

class Animal {
    private $name;

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

    public function getName() {
        return $this->name;
    }
}

$animal = new Animal("Elephant");
echo $animal->getName();
                
Output: Elephant

Inheritance

Inheritance is a way to create a new class using an existing class. The new class, known as the child class, inherits properties and methods from the parent class.

Example

class Vehicle {
    public $brand;

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

    public function getBrand() {
        return $this->brand;
    }
}

class Bike extends Vehicle {
    public function message() {
        return "This is a " . $this->getBrand() . " bike.";
    }
}

$bike = new Bike("Honda");
echo $bike->message();
                
Output: This is a Honda bike.