0
0
PHPprogramming~10 mins

Abstract classes and methods in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Abstract classes and methods
Define abstract class
Declare abstract method(s)
Create subclass
Implement abstract method(s)
Instantiate subclass object
Call implemented method(s)
Program runs concrete behavior
This flow shows how an abstract class defines methods without code, subclasses must implement them, and then objects can be created to run concrete code.
Execution Sample
PHP
<?php
abstract class Animal {
  abstract public function makeSound();
}

class Dog extends Animal {
  public function makeSound() {
    echo "Woof!\n";
  }
}

$dog = new Dog();
$dog->makeSound();
?>
Defines an abstract class Animal with an abstract method makeSound, then Dog implements it and outputs 'Woof!'.
Execution Table
StepActionEvaluationResult
1Define abstract class Animal with abstract method makeSound()Class Animal created with abstract methodNo object created yet
2Define class Dog extends AnimalDog must implement makeSound()Class Dog created
3Implement makeSound() in DogMethod outputs 'Woof!'Dog class ready for instantiation
4Instantiate Dog object$dog = new Dog()Object $dog created
5Call $dog->makeSound()Executes Dog's makeSound()Outputs: Woof!
6End of programNo more codeProgram ends
💡 Program ends after calling implemented abstract method in subclass
Variable Tracker
VariableStartAfter Step 4After Step 5Final
$dogundefinedObject of class DogObject of class DogObject of class Dog
Key Moments - 3 Insights
Why can't we create an object of the abstract class Animal?
Because abstract classes are incomplete by design (see Step 1 in execution_table). They have abstract methods without code, so PHP forbids creating objects directly from them.
What happens if Dog does not implement the abstract method makeSound()?
PHP will give an error when defining Dog (Step 2) because all abstract methods must be implemented in subclasses.
Why does calling makeSound() on $dog output 'Woof!'?
Because Dog provides the concrete code for makeSound() (Step 5), so calling it runs Dog's version.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the state of $dog after Step 4?
AObject of class Dog
BUndefined
CObject of class Animal
DNull
💡 Hint
Check variable_tracker row for $dog after Step 4
At which step does the program output 'Woof!'?
AStep 4
BStep 5
CStep 3
DStep 6
💡 Hint
Look at execution_table Action and Result columns for output
If Dog did not implement makeSound(), what would happen?
AProgram runs normally
BError when instantiating Dog
CError when defining Dog class
DOutput would be empty
💡 Hint
Refer to key_moments about missing implementation and Step 2 in execution_table
Concept Snapshot
abstract class ClassName {
  abstract public function methodName();
}

- Abstract classes cannot be instantiated.
- Abstract methods have no body and must be implemented in subclasses.
- Subclasses must define all abstract methods.
- Objects are created from subclasses, not abstract classes.
Full Transcript
This example shows how PHP uses abstract classes and methods. First, an abstract class Animal is defined with an abstract method makeSound. This method has no code and forces subclasses to implement it. Then, a subclass Dog extends Animal and provides the makeSound method that outputs 'Woof!'. We cannot create an Animal object directly because it is abstract. Instead, we create a Dog object and call its makeSound method, which prints 'Woof!'. This ensures a common interface while allowing different subclasses to provide their own behavior.