0
0
PHPprogramming~20 mins

Constructor promotion in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Constructor Promotion Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of a class using constructor promotion
What is the output of this PHP code using constructor promotion?
PHP
<?php
class User {
    public function __construct(
        public string $name,
        public int $age
    ) {}
}
$user = new User("Alice", 30);
echo $user->name . ' is ' . $user->age . ' years old.';
AAlice is years old.
BAlice is 30 years old.
CError: Property age is not defined
D30 is Alice years old.
Attempts:
2 left
💡 Hint
Constructor promotion automatically creates and assigns properties.
Predict Output
intermediate
2:00remaining
Output when mixing promoted and non-promoted properties
What will this PHP code output?
PHP
<?php
class Product {
    public string $category;
    public function __construct(
        public string $name,
        public float $price
    ) {
        $this->category = "General";
    }
}
$p = new Product("Book", 12.5);
echo $p->name . ' costs $' . $p->price . ' in category ' . $p->category;
ABook costs $12.5 in category General
BBook costs $ in category General
CError: Property category is not initialized
D12.5 costs $Book in category General
Attempts:
2 left
💡 Hint
Non-promoted properties must be initialized manually.
🔧 Debug
advanced
2:00remaining
Identify the error in constructor promotion usage
What error does this PHP code produce?
PHP
<?php
class Car {
    public function __construct(
        private string $make,
        protected int $year = 2020,
        public $model
    ) {}
}
$car = new Car("Toyota", 2018, "Corolla");
echo $car->make;
ANotice: Undefined property: Car::$make
BParse error: Default value for parameter must be constant
CFatal error: Cannot access private property Car::$make
DNo error, outputs: Toyota
Attempts:
2 left
💡 Hint
Private properties cannot be accessed directly outside the class.
📝 Syntax
advanced
2:00remaining
Which option causes a syntax error in constructor promotion?
Which of these constructor declarations will cause a syntax error in PHP?
Apublic function __construct(public int $x = 5, private string $y) {}
Bpublic function __construct(public int $x, private string $y = "default") {}
Cpublic function __construct(public int $x, private string $y) {}
Dpublic function __construct(public int $x = 5, private string $y = "default") {}
Attempts:
2 left
💡 Hint
In PHP, parameters with default values must come after parameters without defaults.
🚀 Application
expert
2:00remaining
Count properties created by constructor promotion
How many properties does this PHP class have after construction?
PHP
<?php
class Employee {
    public string $department = "HR";
    public function __construct(
        public string $name,
        private int $id,
        protected float $salary = 50000.0
    ) {}
}
$emp = new Employee("John", 1234);
A3
B5
C2
D4
Attempts:
2 left
💡 Hint
Count all declared properties including promoted and non-promoted.