What is New in PHP 8?

PHP 8 introduced various improvements and new features to type hinting, ensuring better code efficiency and clarity. These features allow developers to define the expected data types for function parameters, return values, and class properties.

  1. Union Types in PHP 8
    Union types in PHP 8 allow specifying multiple possible types for a function argument, return type, or class property by using a vertical bar (|) to separate the types. This enables greater flexibility while maintaining type safety.

Syntax:

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

Example:

function printValue(int|string $value): void {
    echo $value;
}

printValue(10);    // Output: 10
printValue("Hello"); // Output: Hello
  1. Mixed Type Declaration
    PHP 8 introduced the mixed type, which accepts any data type as input. It is especially useful when you want to pass any kind of data while maintaining type hints for other arguments. This approach provides flexibility similar to dynamic languages like JavaScript.

Syntax:

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

Example:

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

process("hello");   // Output: HELLO
process(5);         // Output: 10
process(['a', 'b']); // Output: Unsupported type
  1. Static Return Type
    PHP 8 allows methods to return a static type, meaning that the return type will be the same as the class in which the method is defined. This enhances support for method chaining and fluent interfaces.

Syntax:

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

Example:

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

$counter = new Counter();
$counter->increment()->increment();
echo $counter->getCount(); // Output: 2
  1. Contravariant Parameters in Inheritance
    PHP 8 allows contravariant parameter types in overridden methods, meaning a subclass method can have a parameter type that is a superclass of the parent class parameter type. This makes method overriding more flexible.

Example:

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

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

$dog = new Dog();
$dog->speak("Woof!");  // Output: Dog barks: Woof!
  1. Private Methods and Parameter Contravariance
    PHP 8 extends contravariant parameter support to private methods in subclasses. This relaxation allows more flexible method overriding while preserving type safety.

Example:

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";
    }
}

$child = new ChildClass();
$child->process(10);  // Output: ChildClass processing: 10

Keep Learning 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *