Covariant Return Types in PHP

Covariant return types in PHP allow a child class method to return a more specific subtype than the return type declared in the parent class method.

This feature was introduced in PHP 7.4 and is commonly used in PHP OOP to make code more precise and type-safe. Covariant return types provide several benefits in PHP:

  • Improve type safety and readability.
  • Make method return values more specific and meaningful results.
  • Reduce the need for explicit type checking or casting.
  • Better follow the Liskov Substitution Principle.
  • Easier debugging.

Syntax of Covariant Return Types in PHP


The basic syntax to use covariant return types in PHP is as:

class ParentClass {
   public function methodName(): ParentType {
     // code
   }
}
class ChildClass extends ParentClass {
   public function methodName(): ChildType {
     // code
   }
}

In the above syntax:

  • ParentType specifies the return type of the parent class method.
  • ChildType must be a subclass of ParentType.
  • The method name must be exactly the same in both classes.
  • The access modifier should be the same or less restrictive in the child class.
  • The return type must follow PHP inheritance and covariance rules.

Examples of Covariant Return Types in PHP


Let us take some important examples based on the covariant return types in PHP.
[blocksy-content-block id=”12371″]

Example 1: Basic Use of Covariant Return Types

<?php
// Parent class
class Animal {
 // Method in the parent class that returns an Animal object.
    public function getAnimal(): Animal {
       return new Animal();
    }
}
// Child class that extends the parent class.
class Dog extends Animal {
 // Child class overriding the getAnimal() method.
 // Returning a more specific type (Dog instead of Animal).
    public function getAnimal(): Dog {
       return new Dog();
    }
}
// Creating an object of the child class.
$dog = new Dog();

// Calling the overridden method.
$animal = $dog->getAnimal();

// Displaying information about the returned object.
var_dump($animal);
?>

Output:

object(Dog)#2 (0) {
}

In this example:

  • Animal is the parent class, which contains a method getAnimal() that returns an object of type Animal. The return type is specified using : Animal.
  • Dog is a child class that extends the Animal class. The Dog class overrides the getAnimal() method.
  • The overridden method returns a Dog object, which is a subclass of Animal.
  • Since Dog is a subclass (or type) of Animal, PHP allows this because the child class returns a more specific type, not a broader one. This is called a covariant return type.
  • When the method is called using a Dog object, getAnimal() of class Dog is executed.
  • The var_dump() method confirms that the returned object is of type Dog.
    • object(Dog) shows that the returned object is of class Dog.
    • #2 is the object ID, which may vary each time the script runs.
    • (0) means the object has no properties.
    • { } represents an empty object structure.

This example confirms that the overridden method returns a Dog object, which demonstrates the use of covariant return types in PHP.
[blocksy-content-block id=”12121″]

Example 2: (Not Covariant)

<?php
class Animal {
   public function getAnimal(): Dog {
     return new Dog();
   }
}
class Dog extends Animal {
  public function getAnimal(): Animal {
    return new Animal();
  }
}
?>

This code will cause a fatal error because the child class is returning a less specific (broader) type.

Example 3: Covariant Return Types with Method Overriding

<?php
class Person { }
class Student extends Person { }

class PersonProvider {
 // Method that returns a Person object
    public function getPerson(): Person {
      return new Person();
    }
}
class StudentProvider extends PersonProvider {
 // The child class overrides the getPerson() method, which returns a more specific type.
    public function getPerson(): Student {
       return new Student();
    }
}
?>

In this example:

  • Person is the base class.
  • Student is a child class that extends Person. This means a Student is a type of Person.
  • PersonProvider is the parent class, which contains a method getPerson().
  • The method returns an object of type Person. The return type is declared using : Person.
  • StudentProvider is a child class that extends PersonProvider.
  • The getPerson() method in StudentProvider overrides the parent class method. The overridden method returns a Student object.

This example demonstrates inheritance, method overriding, covariant return types, and type safety in PHP.


Conclusion

Covariant return types in PHP are a modern feature that makes object-oriented programming more powerful and type-safe. They allow child classes to return more specific objects while maintaining compatibility with parent class methods. We hope that you have understood the basic concepts of covariant types and practiced all examples.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.