0
0
PHPprogramming~10 mins

Class declaration syntax 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 class named Car.

PHP
<?php
class [1] {
}
?>
Drag options to blanks, or click blank then click option'
ACar
Bcar
Cvehicle
DCarClass
Attempts:
3 left
💡 Hint
Common Mistakes
Using lowercase class names which is against convention.
Using unrelated names that don't describe the class.
2fill in blank
medium

Complete the code to add a public property named $color to the class.

PHP
<?php
class Bike {
    public [1];
}
?>
Drag options to blanks, or click blank then click option'
A$speed
B$type
C$color
D$weight
Attempts:
3 left
💡 Hint
Common Mistakes
Using property names other than $color.
Forgetting the dollar sign ($) before the property name.
3fill in blank
hard

Fix the error in the class declaration by completing the code.

PHP
<?php
[1] Vehicle {
}
?>
Drag options to blanks, or click blank then click option'
Aclass
Bfunction
Cvar
Dpublic
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'function' instead of 'class' for class declaration.
Omitting the 'class' keyword entirely.
4fill in blank
hard

Fill both blanks to declare a class named Person with a private property $name.

PHP
<?php
[1] Person {
    [2] $name;
}
?>
Drag options to blanks, or click blank then click option'
Aclass
Bpublic
Cprivate
Dfunction
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'public' instead of 'private' for the property.
Using 'function' instead of 'class' for the class declaration.
5fill in blank
hard

Fill all three blanks to declare a class named Animal with a protected property $species and a public method speak().

PHP
<?php
[1] Animal {
    [2] $species;
    [3] function speak() {
        echo "Hello!";
    }
}
?>
Drag options to blanks, or click blank then click option'
Aclass
Bprotected
Cpublic
Dprivate
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong visibility keywords for properties or methods.
Forgetting to declare the class with the keyword class.