Open In App

What is New in PHP Type Hinting Support in PHP 8?

Last Updated : 03 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

PHP 8 introduced several enhancements and new features related to type hinting. That will check the code for their respective type so that we can use our code efficiently.

These are the following features used in PHP Type Hitting Support:

Union Types

PHP 8 allows specifying multiple possible types for function arguments, return types, and class properties using union types. This is achieved by separating the types with a vertical bar (|).

Syntax:

function foo(int|string $value): void {
    // Function implementation
}

Example: This example shows the union type.

PHP
// Function accepting an integer or string
function printValue(int|string $value): void {
    echo $value;
}

// Usage
printValue(10);    // Output: 10
printValue("Hello"); // Output: Hello

Output:

10
Hello

Mixed Type

PHP 8 introduces a new mixed type declaration. This type allows any type to be passed, similar to the dynamic typing in languages like JavaScript. It's useful when you want to accept any type but still want to maintain type safety for other arguments.

Syntax:

function bar(mixed $value): void {
    // Function implementation
}

Example: This example shows the mixed type.

PHP
// Function accepting any type
function process(mixed $value): void {
    if (is_string($value)) {
        echo strtoupper($value);
    } elseif (is_int($value)) {
        echo $value * 2;
    } else {
        echo "Unsupported type";
    }
}

// Usage
process("hello");   // Output: HELLO
process(5);         // Output: 10
process(['a', 'b']); // Output: Unsupported type

Output:

HELLO
10
Unsupported type

Static Return Type

PHP 8 introduced the ability to specify the return type as static in a class method. This means that the return type will be the same as the class in which the method is defined, providing better support for fluent interfaces and method chaining.

Syntax:

class Example {
    public static function create(): static {
        return new static();
    }
}

Example: This example shows the static return type.

PHP
class Counter {
    private int $count = 0;
    
    public function increment(): static {
        $this->count++;
        return $this;
    }
    
    public function getCount(): int {
        return $this->count;
    }
}

// Usage
$counter = new Counter();
$counter->increment()->increment();
echo $counter->getCount(); 

Output
class Counter {
    private int $count = 0;
    
    public function increment(): static {
        $this->count++;
        return $this;
    }
    
    public function getCount(): int {
        return...

Output:

 2

Inheritance of Contravariant Parameters

In PHP 8, contravariant parameter types are now allowed when overriding methods in subclasses. This means that you can type hint a parameter in a child class method with a type that is a superclass of the type specified in the parent class method. This enhances the flexibility of method overriding while still maintaining type safety.

Example: This example shows the inheritance of contravariant parameters.

PHP
class Animal {
    public function speak(): void {
        echo "Animal speaks";
    }
}

class Dog extends Animal {
    public function speak(string $sound): void {
        echo "Dog barks: $sound";
    }
}

// Usage
$dog = new Dog();
$dog->speak("Woof!"); 

Output:

Dog barks: Woof!

Inheritance of Private Methods with Parameter Contravariance

PHP 8 extends the support for contravariant parameters to private methods in subclasses. Previously, this was not allowed due to potential violations of type safety, but PHP 8 relaxes this restriction.

Example: This example shows the inheritance of Private Methods with Parameter Contravariance.

PHP
class ParentClass {
    private function process(string $data): void {
        echo "ParentClass processing: $data";
    }
}

class ChildClass extends ParentClass {
    private function process(int $data): void {
        echo "ChildClass processing: $data";
    }
}

// Usage
$child = new ChildClass();
$child->process(10); 

Output:

ChildClass processing: 10

Next Article
Article Tags :

Similar Reads