What if you could cut your class code in half and avoid silly mistakes with just one simple trick?
Why Constructor promotion in PHP? - Purpose & Use Cases
Imagine you want to create a class in PHP that holds some data, like a person's name and age. You write a constructor to set these values and also declare properties for them. Doing this for many classes or many properties means writing a lot of repetitive code.
This manual way is slow and boring because you have to write the same lines over and over: declaring properties, writing the constructor parameters, and assigning values inside the constructor. It's easy to make mistakes, like forgetting to assign a value or mismatching names.
Constructor promotion lets you declare and initialize properties directly in the constructor parameters. This means less code, fewer mistakes, and your class looks cleaner and easier to read.
class Person {
public string $name;
public int $age;
public function __construct(string $name, int $age) {
$this->name = $name;
$this->age = $age;
}
}class Person {
public function __construct(public string $name, public int $age) {}
}You can write classes faster and with less chance of errors, making your code cleaner and easier to maintain.
When building a user profile system, you often create many classes with similar properties. Constructor promotion helps you quickly set up these classes without repetitive code.
Constructor promotion reduces repetitive code in class definitions.
It combines property declaration and initialization in one step.
This leads to cleaner, easier-to-read, and less error-prone code.
Practice
constructor promotion in PHP classes?Solution
Step 1: Understand constructor promotion purpose
Constructor promotion lets you declare and assign properties directly in the constructor parameters.Step 2: Compare options
Only It allows declaring and assigning properties in one step inside the constructor. correctly describes this benefit. Options B, C, and D describe unrelated features.Final Answer:
It allows declaring and assigning properties in one step inside the constructor. -> Option BQuick Check:
Constructor promotion = Declare and assign properties together [OK]
- Thinking it auto-generates getters/setters
- Confusing with immutability
- Assuming it enforces typing
Solution
Step 1: Identify visibility keywords in constructor parameters
Constructor promotion requires visibility keywords like private, protected, or public before parameters.Step 2: Check each option
public function __construct(private string $name, private int $age) {} uses private for both parameters, which is correct. public function __construct(private string $name, int $age) {} misses visibility for $age. public function __construct(string $name, int $age) { $this->name = $name; } is normal assignment, not promotion. public function __construct(var $name, var $age) {} uses invalid 'var' keyword.Final Answer:
public function __construct(private string $name, private int $age) {} -> Option DQuick Check:
Visibility + parameter = constructor promotion [OK]
- Omitting visibility keyword
- Using invalid keywords like var
- Confusing promotion with manual assignment
class User {
public function __construct(private string $name, private int $age) {}
public function getInfo() {
return "$this->name is $this->age years old.";
}
}
$user = new User('Alice', 30);
echo $user->getInfo();Solution
Step 1: Understand property promotion and access
The properties $name and $age are private but accessible inside class methods like getInfo().Step 2: Analyze getInfo() output
It returns a string combining $name and $age values. Since $user is created with 'Alice' and 30, output is "Alice is 30 years old."Final Answer:
Alice is 30 years old. -> Option AQuick Check:
Private properties accessible inside class = correct output [OK]
- Assuming private properties are inaccessible inside class
- Mixing order of name and age in output
- Expecting syntax error
class Product {
public function __construct(private $name, private int $price) {}
}Solution
Step 1: Check parameter declarations
The $name parameter lacks a type declaration, which is required for constructor promotion to be clear and strict.Step 2: Validate other options
Private visibility is allowed, int type for price is valid, and promotion supports all visibility levels.Final Answer:
Missing type declaration for $name parameter. -> Option CQuick Check:
All promoted parameters need type declarations [OK]
- Omitting type for promoted parameters
- Thinking only public visibility allowed
- Confusing parameter types
Book with promoted properties title (string), author (string), and pages (int). You also want to add a method summary() that returns a string like "Title by Author, Pages pages.". Which of the following is the correct complete class code?Solution
Step 1: Check constructor promotion syntax
class Book { public function __construct(public string $title, public string $author, public int $pages) {} public function summary() { return "$this->title by $this->author, $this->pages pages."; } } correctly uses public visibility with types in constructor parameters to promote properties.Step 2: Verify summary() method access and string
class Book { public function __construct(public string $title, public string $author, public int $pages) {} public function summary() { return "$this->title by $this->author, $this->pages pages."; } } uses $this->property to access promoted properties and returns the correct formatted string.Step 3: Analyze other options
class Book { public string $title; public string $author; public int $pages; public function __construct($title, $author, $pages) { $this->title = $title; $this->author = $author; $this->pages = $pages; } public function summary() { return "$title by $author, $pages pages."; } } uses manual assignment but summary() incorrectly uses variables without $this->. class Book { public function __construct(string $title, string $author, int $pages) {} public function summary() { return "$this->title by $this->author, $this->pages pages."; } } promotes parameters but does not assign them to properties. class Book { public function __construct(private string $title, private string $author, private int $pages) {} public function summary() { return "$title by $author, $pages pages."; } } uses private properties but summary() accesses variables without $this->, causing errors.Final Answer:
Option A code with public promoted properties and correct summary() -> Option AQuick Check:
Promotion + correct property access = class Book { public function __construct(public string $title, public string $author, public int $pages) {} public function summary() { return "$this->title by $this->author, $this->pages pages."; } } [OK]
- Forgetting $this-> in methods
- Not assigning promoted properties properly
- Mixing private properties with wrong access
Ready to prove it?
Pass the test and this pattern moves unknown
