OOP in PHP: Classes, Objects and Inheritance
Create a "Producto" object and see how its methods transform its own data
🎯 Live Demo
Instantiated object:
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
extendsreuses code from another classparent::calls the parent's method
💪 Practical Exercise
Extend the class Producto
- Add a method
estaEnStock(): boolthat returns whether$stock > 0. - Create a second child class
ProductoOferta extends Productothat applies a 15% discount automatically in the constructor. - Add an interface
Facturablewith a methodcalcularFactura()and implement it inProducto.
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;
}
}