Key New Features in PHP 8: Named Parameters, Union Types, Match Expressions, and More

This article introduces the most important PHP 8 language features—including named arguments, union types, match expressions, the nullsafe operator, attributes, constructor property promotion, weak maps, static return types, throw expressions, the Stringable interface, get_debug_type, string helper functions, fdiv, and error‑suppression—providing explanations and code examples for each.

php Courses
php Courses
php Courses
Key New Features in PHP 8: Named Parameters, Union Types, Match Expressions, and More

1. Named Parameters: Allows developers to pass arguments to functions by name instead of position, improving flexibility and readability.

function greet($name, $age) {
    echo "Hello $name, you are $age years old.";
}

greet(age: 25, name: "John");

2. Union Types: Enables specifying multiple possible types for function parameters, return types, etc., enhancing flexibility and reusability.

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

3. Match Expression: A more concise and expressive alternative to the switch statement for value matching.

$result = match($value) {
    1 => "One",
    2 => "Two",
    default => "Other",
};

4. Nullsafe Operator ( ?-> ): Simplifies accessing properties or methods on potentially null objects, reducing boilerplate null checks. $length = $obj?->getProperty()?->getLength(); 5. Attributes: A powerful tool for adding metadata to classes, methods, and functions, allowing behavior customization.

#[Route("/user/{id}")]
class UserController {
    #[HttpGet("/info")]
    public function getInfo($id) { }
}

6. Constructor Property Promotion: Concise syntax for declaring and initializing properties directly in the constructor signature.

class Person {
    public function __construct(
        public string $name,
        public int $age
    ) {}
}

7. WeakMap: A special map that allows objects to be used as keys without preventing their garbage collection, helping avoid memory leaks.

$map = new WeakMap();
$map[$obj] = "value";

8. Static Return Type: Allows a method to declare static as its return type, indicating it returns an instance of the called class.

class A {
    public static function getInstance(): static {
        return new static();
    }
}

9. Throw Expression: Enables throwing exceptions directly within expressions, useful in ternary operators.

$result = $value > 0 ? $value : throw new InvalidArgumentException("Invalid value");

10. Stringable Interface: By implementing Stringable, a class can define its own __toString() method for string conversion.

class MyClass implements Stringable {
    public function __toString(): string {
        return "MyClass";
    }
}

11. get_debug_type() Function: Returns a string with detailed type information of a variable, including the exact class name for objects. $type = get_debug_type($var); 12. str_contains() Function: Checks whether a needle string exists within a haystack string, returning true or false.

if (str_contains($haystack, $needle)) {
    echo "Found";
}

13. str_starts_with() and str_ends_with() Functions: Determine if a string starts or ends with a given substring.

if (str_starts_with($string, $prefix)) {
    echo "Starts with prefix";
}

14. fdiv() Function: Performs floating‑point division and returns the result, handling special cases like NaN and infinities. $result = fdiv(10.0, 3.0); 15. Error‑Suppression Operator ( @ ): Allows suppression of non‑exception errors such as warnings, enabling smoother execution of subsequent code.

$value = @some_function(); // Suppress errors
Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendCode Examplesfeatures
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.