0
0
PHPprogramming~10 mins

Constructor promotion in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Constructor promotion
Define class with promoted properties
Create object with constructor args
Properties auto-assigned
Use object properties directly
Constructor promotion lets you declare and assign class properties directly in the constructor parameters, simplifying code.
Execution Sample
PHP
<?php
class User {
  public function __construct(public string $name, public int $age) {}
}
$user = new User("Anna", 30);
echo $user->name . ', ' . $user->age;
?>
This code creates a User object with name and age using constructor promotion, then prints the values.
Execution Table
StepActionEvaluationResult
1Define class User with promoted propertiespublic string $name, public int $ageProperties declared and constructor created
2Create new User object with args ('Anna', 30)$name = 'Anna', $age = 30Object $user created
3Assign constructor args to properties$this->name = 'Anna', $this->age = 30Properties set automatically
4Access $user->nameReturns 'Anna'Output: Anna
5Access $user->ageReturns 30Output: 30
6Concatenate and echo'Anna, 30'Printed: Anna, 30
💡 End of script after printing user name and age
Variable Tracker
VariableStartAfter Step 2After Step 3Final
$nameundefined'Anna''Anna'undefined
$ageundefined3030undefined
$user->nameundefinedundefined'Anna''Anna'
$user->ageundefinedundefined3030
Key Moments - 3 Insights
Why don't we see property declarations inside the class body?
Because constructor promotion declares and assigns properties directly in the constructor parameters, so separate declarations are not needed (see Step 1 and Step 3).
How are the constructor arguments assigned to properties automatically?
The 'public' keyword before parameters tells PHP to create properties and assign values automatically during object creation (Step 3 shows this).
Can we access properties like $user->name right after creating the object?
Yes, because properties are set during construction, so accessing them after Step 3 returns the assigned values.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of $user->age after Step 3?
A30
Bundefined
C0
Dnull
💡 Hint
Check the 'Result' column in Step 3 where properties are assigned.
At which step are the constructor arguments assigned to the object properties?
AStep 2
BStep 4
CStep 3
DStep 1
💡 Hint
Look for the step describing property assignment in the execution table.
If we remove 'public' from the constructor parameters, what changes in the execution?
AObject creation will fail
BProperties won't be created or assigned automatically
CProperties will still be created automatically
DConstructor will not run
💡 Hint
Recall the role of 'public' in constructor promotion from the key moments.
Concept Snapshot
Constructor promotion in PHP lets you declare and assign class properties directly in the constructor parameters.
Use visibility keywords (public/private/protected) before parameters.
This removes the need for separate property declarations and assignments.
Example: __construct(public string $name, public int $age) {}
Properties are set automatically when creating an object.
Full Transcript
Constructor promotion is a PHP feature that simplifies class property declaration and assignment. Instead of declaring properties separately and assigning them inside the constructor, you write the properties directly in the constructor parameters with visibility keywords like public. When you create an object, PHP automatically creates those properties and assigns the passed values. For example, class User with __construct(public string $name, public int $age) {} creates properties name and age and sets them when you do new User('Anna', 30). You can then access $user->name and $user->age directly. This reduces boilerplate code and makes classes cleaner.