Defining Classes and Using Constructor Property Promotion in PHP
This article explains how to define a PHP class with properties and methods, demonstrates traditional class syntax with a Bird example, and shows how PHP 8's constructor property promotion and default values simplify class definitions while improving readability and efficiency.
When studying PHP, especially for project development, defining classes is an essential step; a class serves as a blueprint that encapsulates data and functionality, and PHP offers robust features for handling classes, constructors, objects, and methods.
Consider a simple example: a class named Bird with two properties, name and voice .
In traditional PHP syntax the class is defined as follows:
<code>class Bird {
private string $name;
private string $voice;
function __construct($name, $voice) {
$this->name = $name;
$this->voice = $voice;
}
function name() {
return $this->name;
}
function voice() {
return $this->voice;
}
}
$bird = new Bird("鸭子", "嘎嘎");
print_r($bird);
</code>In this snippet the Bird class defines two private attributes, name and voice . The __construct() method (constructor) initializes these attributes, and the voice() method returns the bird's sound.
Instantiating the class with the values "鸭子" (duck) and "嘎嘎" (quack) and printing the object displays those properties.
PHP 8.0 introduced constructor property promotion, which streamlines attribute initialization when properties are assigned only via the constructor.
Refactoring the Bird class with promotion yields:
<code><?php
class Bird {
function __construct(
private string $name,
private string $voice
) {}
function name() {
return $this->name;
}
function voice() {
return $this->voice;
}
}
$bird = new Bird("鸭子", "嘎嘎");
print_r($bird);
?>
</code>With promotion, the name and voice attributes are initialized directly in the constructor's parameter list, eliminating the need for explicit property declarations and assignments inside the constructor body; a name() method is added for accessing the bird's name.
Default values can still be assigned in the constructor as before. For example, the voice attribute can have a default value "Beep":
<code>class Bird {
function __construct(
private string $name,
private string $voice = "Beep"
) {}
}
</code>The advantages of constructor property promotion include:
More concise code: reduces boilerplate, making class definitions cleaner and more readable.
Efficiency: simplifies attribute initialization, especially for classes with many properties.
Encapsulation: retains the benefits of private properties while simplifying initialization.
Constructor property promotion is another example of PHP's evolving capabilities, making the language more powerful and easier to use for both simple tasks and complex systems.
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.