New Features in PHP 8: Constructor Property Promotion and Reducing Redundant Code
The article explains PHP 8's constructor property promotion and other features such as named arguments and the null coalescing operator, showing how they simplify class definitions, reduce redundant getter/setter code, and improve readability and maintainability for backend developers.
PHP 8, released on November 26 2020, introduces many exciting features for web development, including constructor property promotion and tools that reduce repetitive code. This article demonstrates how to use these features with examples.
1. Constructor Property Promotion
In earlier PHP versions, developers had to manually define getter and setter methods for class properties. PHP 8 allows the use of constructor property promotion to simplify this process.
Consider the following example where a Person class defines name and age properties with explicit getters and setters:
<code>class Person {
public string $name;
public int $age;
public function getName(): string {
return $this->name;
}
public function setName(string $name): void {
$this->name = $name;
}
public function getAge(): int {
return $this->age;
}
public function setAge(int $age): void {
$this->age = $age;
}
}
</code>With constructor property promotion, the same class can be written more concisely:
<code>class Person {
public function __construct(
public string $name,
public int $age,
) {}
}
</code>Now the getter and setter methods are unnecessary, and the properties can be accessed directly:
<code>$person = new Person('John Doe', 25);
echo $person->name; // outputs: John Doe
echo $person->age; // outputs: 25
</code>This demonstrates how property promotion greatly simplifies code structure and eliminates redundancy.
2. Reducing Redundant Code
PHP 8 also adds features that help reduce code duplication, such as named arguments and the null coalescing operator.
Named Arguments
Named arguments allow function calls to specify parameter names, improving readability and flexibility.
Example of a simple sum function:
<code>function sum(int $a, int $b): int {
return $a + $b;
}
echo sum(5, 10); // outputs: 15
</code>Using named arguments, the same call can be written as:
<code>echo sum(b: 5, a: 10); // outputs: 15
</code>This lets developers pass arguments in any order.
Null Coalescing Operator
Previously, checking for null and providing a default required ternary operators or if statements.
Traditional approach:
<code>$username = isset($_GET['username']) ? $_GET['username'] : 'Guest';
</code>PHP 8 simplifies this with the null coalescing operator ( ?? ):
<code>$username = $_GET['username'] ?? 'Guest';
</code>Additional new features in PHP 8 include the match expression and static return type declarations, further enhancing the language's capabilities.
Conclusion
The article covered two major PHP 8 features: constructor property promotion, which makes class definitions more concise by removing the need for explicit getters and setters, and tools like named arguments and the null coalescing operator that improve code readability and reduce duplication.
php中文网 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.