PHP Classes and Objects

Learn how to define classes and create objects in PHP, along with their properties and methods.

Introduction to Classes and Objects

In PHP, a class is a blueprint for creating objects. An object is an instance of a class that encapsulates properties (attributes) and methods (functions) that define its behavior. Understanding classes and objects is essential for leveraging the power of object-oriented programming in PHP.

Defining a Class

To define a class in PHP, use the class keyword followed by the class name. The class can contain properties and methods.

<?php
class Car {
    // Properties
    public $color;
    public $model;

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

    // Method
    public function display() {
        return "Car model: " . $this->model . ", Color: " . $this->color;
    }
}
?>

Creating Objects

To create an object from a class, use the new keyword followed by the class name. You can then access the object's properties and methods using the arrow operator (->).

<?php
$myCar = new Car("red", "Toyota");
echo $myCar->display(); // Outputs: Car model: Toyota, Color: red
?>

Access Modifiers

Access modifiers control the visibility of class properties and methods. PHP provides three access modifiers:

  • public: Accessible from anywhere.
  • protected: Accessible within the class and by inheriting classes.
  • private: Accessible only within the class itself.

Example of Access Modifiers

<?php
class Person {
    public $name;
    protected $age;
    private $ssn;

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

    public function getAge() {
        return $this->age;
    }
}

$person = new Person("John", 30, "123-45-6789");
echo $person->name; // Accessible
echo $person->getAge(); // Accessible
// echo $person->ssn; // Not accessible (private)
?>

Inheritance

Inheritance allows one class (child class) to inherit properties and methods from another class (parent class). This promotes code reuse and establishes a relationship between classes.

<?php
class Vehicle {
    public $type;

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

    public function getType() {
        return $this->type;
    }
}

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

$myBike = new Bike("mountain");
echo $myBike->getBikeInfo(); // Outputs: This is a mountain bike.
?>

Polymorphism

Polymorphism allows methods to have the same name but behave differently based on the object that it is acting upon.

<?php
class Animal {
    public function sound() {
        return "Some sound";
    }
}

class Dog extends Animal {
    public function sound() {
        return "Bark";
    }
}

class Cat extends Animal {
    public function sound() {
        return "Meow";
    }
}

function animalSound(Animal $animal) {
    echo $animal->sound();
}

$dog = new Dog();
$cat = new Cat();

animalSound($dog); // Outputs: Bark
animalSound($cat); // Outputs: Meow
?>

Conclusion

Understanding classes and objects in PHP is crucial for applying object-oriented programming principles. By mastering these concepts, you can create robust and maintainable applications that are easier to extend and modify.