0
0
PHPprogramming~10 mins

Why OOP is needed in PHP - Test Your Understanding

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

Complete the code to define a class in PHP.

PHP
<?php
class [1] {
}
?>
Drag options to blanks, or click blank then click option'
Avariable
Bfunction
Cecho
DCar
Attempts:
3 left
💡 Hint
Common Mistakes
Using keywords like 'function' or 'echo' as class names.
Leaving the class name blank.
2fill in blank
medium

Complete the code to create an object from a class.

PHP
<?php
$car = new [1]();
?>
Drag options to blanks, or click blank then click option'
ACar
Bfunction
Cecho
Dvariable
Attempts:
3 left
💡 Hint
Common Mistakes
Using keywords like 'function' instead of a class name.
Forgetting the parentheses after the class name.
3fill in blank
hard

Fix 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'
Astart
Becho
CCar
Dvariable
Attempts:
3 left
💡 Hint
Common Mistakes
Using class name as method name.
Using PHP keywords as method names.
4fill in blank
hard

Complete 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'
A->
B()
C;
D.
Attempts:
3 left
💡 Hint
Common Mistakes
Using dot . instead of arrow ->.
Forgetting parentheses when calling methods.
5fill in blank
hard

Fill 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'
Astring
B$color
Ccolor
Dint
Attempts:
3 left
💡 Hint
Common Mistakes
Using property name without $.
Assigning property to wrong variable.