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.
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 errorsphp中文网 Courses
php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.