WikiGalaxy

Personalize

PHP OOP: Classes and Objects

Understanding Classes and Objects:

In PHP, a class is a blueprint for objects. It defines properties and methods that the created objects will have. An object is an instance of a class.


<?php
class Car {
    public $color;
    public $model;
    
    public function __construct($color, $model) {
        $this->color = $color;
        $this->model = $model;
    }
    
    public function message() {
        return "My car is a " . $this->color . " " . $this->model . "!";
    }
}

$myCar = new Car("black", "Volvo");
echo $myCar->message();
?>
    

Console Output:

My car is a black Volvo!

PHP OOP: Inheritance

Inheritance in PHP:

Inheritance allows a class to use methods and properties of another class. The class that inherits is called the subclass, and the class being inherited from is the superclass.


<?php
class Fruit {
    public $name;
    public $color;
    
    public function __construct($name, $color) {
        $this->name = $name;
        $this->color = $color;
    }
    
    public function intro() {
        echo "The fruit is {$this->name} and the color is {$this->color}.";
    }
}

class Strawberry extends Fruit {
    public function message() {
        echo "Am I a fruit or a berry?";
    }
}

$strawberry = new Strawberry("Strawberry", "red");
$strawberry->message();
$strawberry->intro();
?>
    

Console Output:

Am I a fruit or a berry? The fruit is Strawberry and the color is red.

PHP OOP: Access Modifiers

Access Modifiers in PHP:

PHP provides three types of access modifiers: public, private, and protected. These define the visibility of class properties and methods.


<?php
class Fruit {
    public $name;
    protected $color;
    private $weight;

    public function setName($n) {
        $this->name = $n;
    }
    
    protected function setColor($c) {
        $this->color = $c;
    }
    
    private function setWeight($w) {
        $this->weight = $w;
    }
}
?>
    

PHP OOP: Polymorphism

Polymorphism in PHP:

Polymorphism allows methods to do different things based on the object it is acting upon, even if they share the same name.


<?php
interface Animal {
    public function makeSound();
}

class Cat implements Animal {
    public function makeSound() {
        echo "Meow";
    }
}

class Dog implements Animal {
    public function makeSound() {
        echo "Bark";
    }
}

$cat = new Cat();
$dog = new Dog();
$cat->makeSound();
$dog->makeSound();
?>
    

Console Output:

Meow Bark

PHP OOP: Abstract Classes

Abstract Classes in PHP:

An abstract class is a class that cannot be instantiated on its own and must be extended by other classes. It can contain abstract methods that must be defined in the child classes.


<?php
abstract class Car {
    public $name;
    public function __construct($name) {
        $this->name = $name;
    }
    abstract public function intro();
}

class Audi extends Car {
    public function intro() {
        return "Choose German quality! I'm an $this->name!";
    }
}

$audi = new Audi("Audi");
echo $audi->intro();
?>
    

Console Output:

Choose German quality! I'm an Audi!

PHP OOP: Interfaces

Interfaces in PHP:

An interface allows you to specify what methods a class should implement. Interfaces are declared with the interface keyword.


<?php
interface Animal {
    public function makeSound();
}

class Lion implements Animal {
    public function makeSound() {
        echo "Roar";
    }
}

$lion = new Lion();
$lion->makeSound();
?>
    

Console Output:

Roar

PHP OOP: Traits

Traits in PHP:

Traits are used to declare methods that can be used in multiple classes. Traits are similar to classes, but they are intended to group functionality in a fine-grained and consistent way.


<?php
trait message1 {
    public function msg1() {
        echo "OOP is fun! ";
    }
}

trait message2 {
    public function msg2() {
        echo "OOP reduces code duplication!";
    }
}

class Welcome {
    use message1;
    use message2;
}

$obj = new Welcome();
$obj->msg1();
$obj->msg2();
?>
    

Console Output:

OOP is fun! OOP reduces code duplication!

PHP OOP: Static Methods

Static Methods in PHP:

Static methods can be called directly - without creating an instance of the class. Static methods are declared with the static keyword.


<?php
class Greeting {
    public static function welcome() {
        echo "Hello World!";
    }
}

Greeting::welcome();
?>
    

Console Output:

Hello World!

PHP OOP: Constructors and Destructors

Constructors and Destructors in PHP:

Constructors are special methods automatically called when an object is created. Destructors are called when the object is destroyed.


<?php
class Car {
    public function __construct() {
        echo "The car is being constructed. ";
    }
    
    public function __destruct() {
        echo "The car is being destroyed.";
    }
}

$car = new Car();
?>
    

Console Output:

The car is being constructed. The car is being destroyed.

logo of wikigalaxy

Newsletter

Subscribe to our newsletter for weekly updates and promotions.

Privacy Policy

 • 

Terms of Service

Copyright © WikiGalaxy 2025