Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create a new instance of the class.
PHP
<?php class Car {} $myCar = [1] Car(); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'create' or 'make' instead of 'new' to instantiate an object.
✗ Incorrect
In PHP, you create a new object by using the new keyword followed by the class name and parentheses.
2fill in blank
mediumComplete the code to instantiate the class and assign it to a variable.
PHP
<?php class Dog {} $dog = [1] Dog(); echo get_class($dog); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using other verbs like 'make' or 'create' which are not valid in PHP for object instantiation.
✗ Incorrect
The new keyword is used to create an instance of the Dog class.
3fill in blank
hardFix the error in the code to correctly instantiate the class.
PHP
<?php class Book {} $book = [1] Book(); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the class name as a function without 'new'.
✗ Incorrect
You must use the new keyword before the class name to create an object.
4fill in blank
hardComplete the code to instantiate the class and call its method.
PHP
<?php class Person { function greet() { return "Hello!"; } } $person = [1] Person(); echo $person->greet(); ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '::' instead of '->' to call an instance method.
✗ Incorrect
Use new to create the object and -> to call its method.
5fill in blank
hardFill both blanks to instantiate the class with a constructor and access its property.
PHP
<?php class Animal { public $name; function __construct($name) { $this->name = $name; } } $animal = [1] Animal([2]); echo $animal->name; ?>
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use quotes around the string argument.
Using '.' instead of '->' to access the property.
✗ Incorrect
Use new to create the object, pass the name as a string, and use -> to access the property.