Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Interfaces and Abstract Classes | OOP Principles and Advanced Features
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
Object-Oriented PHP

bookInterfaces and Abstract Classes

The Idea: Defining Contracts and Shared Behavior

In object-oriented PHP, interfaces and abstract classes are used to organize and structure your code in a clear and consistent way.

An interface is like a contract. It only defines method names and parameters that any class implementing it must provide. This ensures that all classes following the interface have the same methods, making your code predictable and easier to use.

An abstract class is a blueprint for other classes. It can have abstract methods, which do not have code and must be implemented in child classes, and concrete methods, which already have code that can be reused or overridden by subclasses. This allows you to share common functionality while still enforcing certain methods to exist in all child classes.

Syntax and Key Keywords

In PHP, interfaces and abstract classes have specific keywords in their syntax that define how they work.

To define an interface, use the interface keyword:

interface Movable {
    public function move($distance);
}

Here, Movable is an interface. Any class that implements this interface must define the move() method. To do this, use the implements keyword:

class Car implements Movable {
    public function move($distance) {
        echo "Car moves $distance miles.";
    }
}

To define an abstract class, use the abstract keyword:

abstract class Transport {
    protected $name;

    abstract public function getType(); // abstract method

    public function displayName() {     // concrete method
        echo "Transport Name: " . $this->name;
    }
}

A class that extends the abstract class must implement all abstract methods using the extends keyword:

class Car extends Transport {
    public function getType() {
        return "Car";
    }
}

Code Example: Using Interfaces and Abstract Classes

Below, you will see how to define an interface Movable, an abstract class Transport, and a concrete class that implements the interface and extends the abstract class.

main.php

main.php

copy
12345678910111213141516171819202122232425262728293031323334353637383940
<?php // Define the interface interface Movable { public function move($distance); } // Define the abstract class abstract class Transport { protected $name; public function __construct($name) { $this->name = $name; } // Abstract method (must be implemented by child class) abstract public function getType(); // Concrete method (shared by all subclasses) public function displayName() { echo "Transport Name: " . $this->name . "\n"; } } // Concrete class that extends Transport and implements Movable class Car extends Transport implements Movable { public function move($distance) { echo $this->name . " moves forward " . $distance . " miles.\n"; } public function getType() { return "Car"; } } // Usage $myCar = new Car("Toyota"); $myCar->displayName(); // Output: Transport Name: Toyota echo $myCar->getType() . "\n"; // Output: Car $myCar->move(50); // Output: Toyota moves forward 50 miles. ?>

This example shows an abstract Transport class with a protected property $name, a concrete method displayName(), and an abstract method getType() that must be implemented by any subclass.

The Movable interface defines a move() method that any implementing class must provide. The Car class extends Transport and implements Movable, providing specific behavior for both getType() and move().

This demonstrates how PHP uses abstract classes for shared functionality and interfaces to enforce consistent method implementation across different classes.

question mark

Which of the following statements are true about interfaces and abstract classes in PHP?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Suggested prompts:

Can you explain the main differences between interfaces and abstract classes in PHP?

Can you give more real-world examples where you would use interfaces versus abstract classes?

How does multiple inheritance work with interfaces and abstract classes in PHP?

bookInterfaces and Abstract Classes

Swipe to show menu

The Idea: Defining Contracts and Shared Behavior

In object-oriented PHP, interfaces and abstract classes are used to organize and structure your code in a clear and consistent way.

An interface is like a contract. It only defines method names and parameters that any class implementing it must provide. This ensures that all classes following the interface have the same methods, making your code predictable and easier to use.

An abstract class is a blueprint for other classes. It can have abstract methods, which do not have code and must be implemented in child classes, and concrete methods, which already have code that can be reused or overridden by subclasses. This allows you to share common functionality while still enforcing certain methods to exist in all child classes.

Syntax and Key Keywords

In PHP, interfaces and abstract classes have specific keywords in their syntax that define how they work.

To define an interface, use the interface keyword:

interface Movable {
    public function move($distance);
}

Here, Movable is an interface. Any class that implements this interface must define the move() method. To do this, use the implements keyword:

class Car implements Movable {
    public function move($distance) {
        echo "Car moves $distance miles.";
    }
}

To define an abstract class, use the abstract keyword:

abstract class Transport {
    protected $name;

    abstract public function getType(); // abstract method

    public function displayName() {     // concrete method
        echo "Transport Name: " . $this->name;
    }
}

A class that extends the abstract class must implement all abstract methods using the extends keyword:

class Car extends Transport {
    public function getType() {
        return "Car";
    }
}

Code Example: Using Interfaces and Abstract Classes

Below, you will see how to define an interface Movable, an abstract class Transport, and a concrete class that implements the interface and extends the abstract class.

main.php

main.php

copy
12345678910111213141516171819202122232425262728293031323334353637383940
<?php // Define the interface interface Movable { public function move($distance); } // Define the abstract class abstract class Transport { protected $name; public function __construct($name) { $this->name = $name; } // Abstract method (must be implemented by child class) abstract public function getType(); // Concrete method (shared by all subclasses) public function displayName() { echo "Transport Name: " . $this->name . "\n"; } } // Concrete class that extends Transport and implements Movable class Car extends Transport implements Movable { public function move($distance) { echo $this->name . " moves forward " . $distance . " miles.\n"; } public function getType() { return "Car"; } } // Usage $myCar = new Car("Toyota"); $myCar->displayName(); // Output: Transport Name: Toyota echo $myCar->getType() . "\n"; // Output: Car $myCar->move(50); // Output: Toyota moves forward 50 miles. ?>

This example shows an abstract Transport class with a protected property $name, a concrete method displayName(), and an abstract method getType() that must be implemented by any subclass.

The Movable interface defines a move() method that any implementing class must provide. The Car class extends Transport and implements Movable, providing specific behavior for both getType() and move().

This demonstrates how PHP uses abstract classes for shared functionality and interfaces to enforce consistent method implementation across different classes.

question mark

Which of the following statements are true about interfaces and abstract classes in PHP?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 4
some-alt