Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a class in PHP.
PHP
<?php class [1] { } ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using keywords like 'function' or 'echo' as class names.
Leaving the class name blank.
✗ Incorrect
In PHP, class is followed by the class name, like Car.
2fill in blank
mediumComplete the code to create an object from a class.
PHP
<?php
$car = new [1]();
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using keywords like 'function' instead of a class name.
Forgetting the parentheses after the class name.
✗ Incorrect
To create an object, use new followed by the class name, like Car.
3fill in blank
hardFix the error in the method declaration inside the class.
PHP
<?php class Car { public function [1]() { echo "Car started"; } } ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using class name as method name.
Using PHP keywords as method names.
✗ Incorrect
Method names should be descriptive verbs like start.
4fill in blank
hardComplete the code to call the method on the object.
PHP
<?php
$car = new Car();
$car->start[1];
?> Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot
. instead of arrow ->.Forgetting parentheses when calling methods.
✗ Incorrect
Use -> to access methods and () to call them.
5fill in blank
hardFill all three blanks to define a property and set it in the constructor.
PHP
<?php class Car { public [1] $color; public function __construct([2] $color) { $this->color = [3]; } } ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using property name without
$.Assigning property to wrong variable.
✗ Incorrect
Properties have types like string. The constructor parameter and assignment use $color.