0
0
PHPprogramming~10 mins

Object instantiation with new 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 create a new instance of the class.

PHP
<?php
class Car {}
$myCar = [1] Car();
?>
Drag options to blanks, or click blank then click option'
Anew
Bcreate
Cmake
Dbuild
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'create' or 'make' instead of 'new' to instantiate an object.
2fill in blank
medium

Complete 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'
Amake
Bcreate
Cnew
Dbuild
Attempts:
3 left
💡 Hint
Common Mistakes
Using other verbs like 'make' or 'create' which are not valid in PHP for object instantiation.
3fill in blank
hard

Fix 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'
A()
B->
C[]
Dnew
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to call the class name as a function without 'new'.
4fill in blank
hard

Complete 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'
Anew
B->
C::
D.
Attempts:
3 left
💡 Hint
Common Mistakes
Using '::' instead of '->' to call an instance method.
5fill in blank
hard

Fill 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'
Anew
B"Lion"
C->
D'Tiger'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to use quotes around the string argument.
Using '.' instead of '->' to access the property.