OOP in PHP: Classes, Objects and Inheritance

Create a "Producto" object and see how its methods transform its own data

🎯 Live Demo

Instantiated object:

resumen()
Teclado mecánico: 12 units at 45€/ud.
precioConIva(21)
54.45 €
aplicarDescuento(10)
New price: 40.5 €
Class used
Producto

Create your own product!

📖 Code Explanation

Class definition

class Producto {
    protected string $nombre;
    protected float $precio;
    protected int $stock;

    public function __construct(string $nombre, float $precio, int $stock) {
        $this->nombre = $nombre;
        $this->precio = $precio;
        $this->stock  = $stock;
    }

    public function precioConIva(float $iva = 21): float {
        return round($this->precio * (1 + $iva / 100), 2);
    }

    public function aplicarDescuento(float $porcentaje): float {
        $this->precio -= $this->precio * $porcentaje / 100;
        return round($this->precio, 2);
    }
}

📝 Properties and protected

What does it do? Declares the data that each "Producto" object will store. Since it's protected, only the class itself (or its children) can modify them directly — this is encapsulation.

💡 Concept: Encapsulation

public: accessible from anywhere.
protected: accessible from the class and its children.
private: only accessible from within the class itself.

📝 The constructor __construct()

What does it do? Runs automatically when an object is created with new Producto(...), initializing its properties.

📝 Inheritance with extends

class ProductoDigital extends Producto {
    private float $tamanioMB;

    public function __construct(string $nombre, float $precio, int $stock, float $tamanioMB) {
        parent::__construct($nombre, $precio, $stock);
        $this->tamanioMB = $tamanioMB;
    }

    public function resumen(): string {
        return parent::resumen() . " (digital download, {$this->tamanioMB} MB)";
    }
}

What does it do? ProductoDigital reuses everything from Producto and overrides resumen(), calling the parent version with parent::resumen() and adding extra information.

💡 Concept: Inheritance and polymorphism

When you check the box "It's a digital product", the same code that calls $producto->resumen() runs a different version depending on the object's actual class — that's polymorphism.

🎯 Key Concepts

Class

  • Template that defines properties and methods
  • Doesn't use memory until instantiated

Object

  • A concrete instance of a class (new)
  • Each object has its own values

Inheritance

  • extends reuses code from another class
  • parent:: calls the parent's method

💪 Practical Exercise

Extend the class Producto

  1. Add a method estaEnStock(): bool that returns whether $stock > 0.
  2. Create a second child class ProductoOferta extends Producto that applies a 15% discount automatically in the constructor.
  3. Add an interface Facturable with a method calcularFactura() and implement it in Producto.

Solution 1

public function estaEnStock(): bool {
    return $this->stock > 0;
}

Solution 2

class ProductoOferta extends Producto {
    public function __construct(string $nombre, float $precio, int $stock) {
        parent::__construct($nombre, $precio, $stock);
        $this->aplicarDescuento(15);
    }
}

Solution 3

interface Facturable {
    public function calcularFactura(): float;
}

class Producto implements Facturable {
    // ...
    public function calcularFactura(): float {
        return $this->precioConIva() * $this->stock;
    }
}