0
0
PHPprogramming~10 mins

Properties and visibility 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 public property named $name.

PHP
<?php
class Person {
    [1] $name;
}
?>
Drag options to blanks, or click blank then click option'
Avar
Bprivate
Cprotected
Dpublic
Attempts:
3 left
💡 Hint
Common Mistakes
Using private or protected which restrict access.
2fill in blank
medium

Complete the code to make the property $age accessible only within the class and its subclasses.

PHP
<?php
class Person {
    [1] $age;
}
?>
Drag options to blanks, or click blank then click option'
Aprivate
Bprotected
Cpublic
Dvar
Attempts:
3 left
💡 Hint
Common Mistakes
Using private which restricts access only to the class itself.
3fill in blank
hard

Fix the error in the code by choosing the correct visibility keyword for the property $salary that should be accessible only inside the class.

PHP
<?php
class Employee {
    [1] $salary;
}
?>
Drag options to blanks, or click blank then click option'
Aprivate
Bprotected
Cvar
Dpublic
Attempts:
3 left
💡 Hint
Common Mistakes
Using public or protected which allow wider access.
4fill in blank
hard

Fill both blanks to declare a protected property $data and initialize it with an empty array.

PHP
<?php
class Container {
    [1] $data = [2];
}
?>
Drag options to blanks, or click blank then click option'
Aprotected
B[]
Carray()
Dpublic
Attempts:
3 left
💡 Hint
Common Mistakes
Using public instead of protected.
Using array() instead of [] (both valid but only one is correct here).
5fill in blank
hard

Fill all three blanks to declare a private property $count, initialize it to zero, and increment it by one inside a method.

PHP
<?php
class Counter {
    [1] $count = [2];

    public function increment() {
        $this->count [3] 1;
    }
}
?>
Drag options to blanks, or click blank then click option'
Aprivate
B0
C+=
Dpublic
Attempts:
3 left
💡 Hint
Common Mistakes
Using public instead of private.
Using =+ instead of += for increment.