0
0
PHPprogramming~10 mins

Constructor promotion in PHP - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a promoted property in the constructor.

PHP
<?php
class User {
    public function __construct(private [1] $name) {}
}
Drag options to blanks, or click blank then click option'
Aarray
Bstring
Cbool
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using int or bool for a text property.
Omitting the type declaration.
2fill in blank
medium

Complete the code to promote two properties in the constructor.

PHP
<?php
class Product {
    public function __construct(private string $name, private [1] $price) {}
}
Drag options to blanks, or click blank then click option'
Afloat
Bstring
Cbool
Darray
Attempts:
3 left
💡 Hint
Common Mistakes
Using int which only holds whole numbers.
Using string for numeric values.
3fill in blank
hard

Fix the error in the constructor promotion syntax.

PHP
<?php
class Order {
    public function __construct(private [1] $id, private int $quantity) {}
}
Drag options to blanks, or click blank then click option'
Aint
Bbool
Cstring
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using string for numeric IDs.
Omitting the type declaration.
4fill in blank
hard

Fill both blanks to promote properties with correct types.

PHP
<?php
class Employee {
    public function __construct(private [1] $name, private [2] $active) {}
}
Drag options to blanks, or click blank then click option'
Astring
Bint
Cbool
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping the types between the two properties.
Using numeric types for text or boolean values.
5fill in blank
hard

Fill all three blanks to promote properties with correct types and visibility.

PHP
<?php
class Book {
    public function __construct([1] string $title, private [2] $pages, protected [3] $available) {}
}
Drag options to blanks, or click blank then click option'
Apublic
Bint
Cbool
Dprivate
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong visibility for $title.
Using wrong types for $pages or $available.