Sobes.tech
Junior — Middle

How can you implement the creation of an instance that uses an abstract class?

sobes.tech AI

Answer from AI

In PHP, an abstract class cannot be instantiated directly. To create an object that uses an abstract class, you need to create a subclass that implements all the abstract methods of the parent, and then instantiate this subclass.

Example:

abstract class Animal {
    abstract public function makeSound();
}

class Dog extends Animal {
    public function makeSound() {
        return "Woof!";
    }
}

$dog = new Dog();
echo $dog->makeSound(); // Outputs: Woof!

Thus, the instance is created through a concrete class that extends the abstract class and implements its methods.