Bird
Raised Fist0
PHPprogramming~10 mins

Constructor method in PHP - Interactive Code Practice

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

Complete the code to define a constructor method in the class.

PHP
<?php
class Car {
    public function [1]() {
        echo "Car created!";
    }
}
$car = new Car();
?>
Drag options to blanks, or click blank then click option'
Acreate
Bconstructor
C__construct
Dinit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'constructor' instead of '__construct'.
Forgetting the double underscores.
2fill in blank
medium

Complete the constructor to accept a parameter and assign it to a property.

PHP
<?php
class Car {
    public $color;
    public function __construct([1]) {
        $this->color = $color;
    }
}
$car = new Car('red');
echo $car->color;
Drag options to blanks, or click blank then click option'
A$color
Bcolor
C$this->color
Dthis->color
Attempts:
3 left
💡 Hint
Common Mistakes
Omitting the dollar sign in the parameter.
Using property syntax in the parameter list.
3fill in blank
hard

Fix the error in the constructor method name.

PHP
<?php
class Bike {
    public function [1]() {
        echo "Bike ready!";
    }
}
$bike = new Bike();
?>
Drag options to blanks, or click blank then click option'
Aconstruct
B__construct
C_construct
Dconstructor
Attempts:
3 left
💡 Hint
Common Mistakes
Using only one underscore.
Using 'constructor' instead of '__construct'.
4fill in blank
hard

Fill both blanks to create a constructor that sets two properties.

PHP
<?php
class Person {
    public $name;
    public $age;
    public function __construct([1], [2]) {
        $this->name = $name;
        $this->age = $age;
    }
}
$person = new Person('Alice', 30);
echo $person->name . ' is ' . $person->age . ' years old.';
Drag options to blanks, or click blank then click option'
A$name
B$age
Cname
Dage
Attempts:
3 left
💡 Hint
Common Mistakes
Using property names without dollar signs as parameters.
Mixing variable and property names.
5fill in blank
hard

Fill all three blanks to create a constructor that sets a property and calls a method.

PHP
<?php
class Dog {
    public $breed;
    public function __construct([1]) {
        $this->breed = $breed;
        $this->[2]();
    }
    private function [3]() {
        echo "Dog breed is set.";
    }
}
$dog = new Dog('Beagle');
Drag options to blanks, or click blank then click option'
A$breed
Bbark
CshowMessage
Ddisplay
Attempts:
3 left
💡 Hint
Common Mistakes
Calling a method that is not defined.
Using method names that don't match.

Practice

(1/5)
1. What is the main purpose of a constructor method in a PHP class?
easy
A. To print the object details
B. To delete an object from memory
C. To automatically set up an object when it is created
D. To create a new class inside the existing class

Solution

  1. Step 1: Understand what a constructor does

    A constructor is a special method that runs automatically when an object is created.
  2. Step 2: Identify the purpose of this automatic setup

    This setup usually initializes properties or runs code needed for the object to work properly.
  3. Final Answer:

    To automatically set up an object when it is created -> Option C
  4. Quick Check:

    Constructor = automatic setup [OK]
Hint: Constructor runs automatically on object creation [OK]
Common Mistakes:
  • Thinking constructor deletes objects
  • Confusing constructor with printing methods
  • Assuming constructor creates new classes
2. Which of the following is the correct way to declare a constructor method in PHP?
easy
A. function __construct() {}
B. function construct() {}
C. function _construct() {}
D. function __destruct() {}

Solution

  1. Step 1: Recall the special constructor name in PHP

    PHP uses the exact name __construct with two underscores at the start.
  2. Step 2: Check each option for correct syntax

    Only function __construct() {} uses the correct spelling and underscores for the constructor.
  3. Final Answer:

    function __construct() {} -> Option A
  4. Quick Check:

    Constructor name = __construct [OK]
Hint: Constructor always named __construct with two underscores [OK]
Common Mistakes:
  • Missing one or both underscores
  • Using 'construct' without underscores
  • Confusing constructor with destructor (__destruct)
3. What will be the output of the following PHP code?
class Car {
  public $color;
  public function __construct($color) {
    $this->color = $color;
  }
  public function getColor() {
    return $this->color;
  }
}
$myCar = new Car('red');
echo $myCar->getColor();
medium
A. red
B. Car
C. null
D. Error

Solution

  1. Step 1: Understand constructor parameter usage

    The constructor sets the property color to the value passed ('red').
  2. Step 2: Check what getColor() returns

    getColor() returns the color property, which was set to 'red'.
  3. Final Answer:

    red -> Option A
  4. Quick Check:

    Constructor sets color = 'red' [OK]
Hint: Constructor sets properties used later [OK]
Common Mistakes:
  • Forgetting to pass argument to constructor
  • Expecting class name as output
  • Assuming property is null without initialization
4. Identify the error in this PHP class constructor code:
class Person {
  public $name;
  public function __construct {
    $this->name = 'Alice';
  }
}
medium
A. Constructor name should be Person
B. Wrong property name
C. Missing semicolon after assignment
D. Missing parentheses after __construct

Solution

  1. Step 1: Check constructor syntax

    In PHP, the constructor method must have parentheses even if no parameters are used.
  2. Step 2: Identify the missing parentheses

    The code misses () after __construct, causing a syntax error.
  3. Final Answer:

    Missing parentheses after __construct -> Option D
  4. Quick Check:

    Constructor needs () even if empty [OK]
Hint: Always add () after __construct even if empty [OK]
Common Mistakes:
  • Omitting parentheses in method declaration
  • Thinking constructor name must match class
  • Forgetting semicolon after statements
5. You want to create a PHP class Book that stores a title and author. The constructor should accept both values and store them. Which code correctly implements this?
hard
A. class Book { public $title, $author; public function Book($title, $author) { $this->title = $title; $this->author = $author; } }
B. class Book { public $title; public $author; public function __construct($title, $author) { $this->title = $title; $this->author = $author; } }
C. class Book { public $title; public $author; public function __construct() { $title = ''; $author = ''; } }
D. class Book { public $title; public $author; public function __construct($title) { $this->title = $title; } }

Solution

  1. Step 1: Check constructor name and parameters

    PHP uses __construct with two parameters $title and $author, matching the requirement.
  2. Step 2: Verify property assignments

    The code assigns both $title and $author to object properties using $this correctly.
  3. Final Answer:

    class Book { public $title; public $author; public function __construct($title, $author) { $this->title = $title; $this->author = $author; } } -> Option B
  4. Quick Check:

    Constructor with all needed parameters and assignments [OK]
Hint: Use __construct with all needed parameters and assign to properties [OK]
Common Mistakes:
  • Using old-style constructor named after class
  • Not passing all required parameters
  • Not assigning parameters to properties
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard