Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
FAQs Support
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

PHP Interfaces and Abstract Classes

Interfaces define a contract that classes must follow. Abstract classes provide partial implementations. Traits allow code reuse across unrelated classes.

Interfaces

An interface declares method signatures without implementations. A class implements an interface and must define all its methods. A class can implement multiple interfaces.

Interfaces
<?php
interface Drawable {
    public function draw(): string;
    public function getArea(): float;
}

interface Colorable {
    public function setColor(string $color): void;
    public function getColor(): string;
}

// Implementing multiple interfaces
class Circle implements Drawable, Colorable {
    private string $color = "black";

    public function __construct(private float $radius) {}

    public function draw(): string {
        return "Drawing circle with radius {$this->radius}";
    }

    public function getArea(): float {
        return M_PI * $this->radius ** 2;
    }

    public function setColor(string $color): void {
        $this->color = $color;
    }

    public function getColor(): string {
        return $this->color;
    }
}

$c = new Circle(5.0);
$c->setColor("red");
echo $c->draw();              // Drawing circle with radius 5
echo round($c->getArea(), 2); // 78.54
echo $c->getColor();          // red
?>

Abstract Classes

An abstract class can have both abstract methods (no body) and concrete methods (with body). It cannot be instantiated directly.

Abstract Classes
<?php
abstract class Shape {
    protected string $color;

    public function __construct(string $color = "black") {
        $this->color = $color;
    }

    // Abstract — must be implemented by child
    abstract public function area(): float;
    abstract public function perimeter(): float;

    // Concrete — shared implementation
    public function describe(): string {
        return sprintf(
            "%s: area=%.2f, perimeter=%.2f, color=%s",
            get_class($this),
            $this->area(),
            $this->perimeter(),
            $this->color
        );
    }
}

class Rectangle extends Shape {
    public function __construct(
        private float $width,
        private float $height,
        string $color = "blue"
    ) {
        parent::__construct($color);
    }

    public function area(): float      { return $this->width * $this->height; }
    public function perimeter(): float { return 2 * ($this->width + $this->height); }
}

$rect = new Rectangle(4, 6, "green");
echo $rect->describe(); // Rectangle: area=24.00, perimeter=20.00, color=green
?>

Traits

Traits allow you to reuse methods across multiple classes without inheritance. A class can use multiple traits.

Traits
<?php
trait Timestampable {
    private ?string $createdAt = null;
    private ?string $updatedAt = null;

    public function setCreatedAt(): void {
        $this->createdAt = date("Y-m-d H:i:s");
    }

    public function setUpdatedAt(): void {
        $this->updatedAt = date("Y-m-d H:i:s");
    }

    public function getTimestamps(): array {
        return ["created" => $this->createdAt, "updated" => $this->updatedAt];
    }
}

trait SoftDeletable {
    private bool $deleted = false;

    public function softDelete(): void { $this->deleted = true; }
    public function restore(): void    { $this->deleted = false; }
    public function isDeleted(): bool  { return $this->deleted; }
}

class Post {
    use Timestampable, SoftDeletable; // use multiple traits

    public function __construct(public string $title) {
        $this->setCreatedAt();
    }
}

$post = new Post("Hello World");
$post->softDelete();
echo $post->isDeleted() ? "deleted" : "active"; // deleted
print_r($post->getTimestamps());
?>

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.