A constructor method is a special function inside a class that runs automatically when a new object is created. It usually sets up initial values or states for the object.
Click to reveal answer
beginner
How do you name a constructor method in PHP 7 and later?
You name the constructor method __construct(). This is a magic method that PHP calls automatically when creating an object.
Click to reveal answer
beginner
What happens if you do not define a constructor in a PHP class?
If no constructor is defined, PHP creates the object without running any setup code automatically. You can still set properties later, but no automatic initialization happens.
Click to reveal answer
beginner
Show a simple example of a PHP class with a constructor that sets a property.
Example:<br><pre><?php
class Car {
public $color;
public function __construct($color) {
$this->color = $color;
}
}
$myCar = new Car('red');
echo $myCar->color; // Outputs: red
?></pre>
Click to reveal answer
beginner
Why use a constructor method instead of setting properties after creating an object?
Using a constructor ensures the object is ready to use immediately after creation. It helps avoid forgetting to set important properties and keeps code cleaner and safer.
Click to reveal answer
What is the correct name for a constructor method in PHP 7+?
A__construct()
Bconstructor()
Cinit()
Dstart()
✗ Incorrect
The magic method __construct() is the standard constructor name in PHP 7 and later.
When is a constructor method called?
AWhen a method is called
BWhen an object is created
CWhen a property is accessed
DWhen the script ends
✗ Incorrect
The constructor runs automatically right after an object is created.
What keyword is used inside a constructor to refer to the current object?
A$this
B$self
C$current
D$obj
✗ Incorrect
In PHP, $this refers to the current object inside class methods.
What happens if you create an object from a class without a constructor?
AThe object cannot be created
BPHP throws an error
CThe object is created without automatic setup
DThe object runs a default constructor
✗ Incorrect
Without a constructor, the object is created but no automatic initialization code runs.
Which of these is a good reason to use a constructor?
ATo call static methods
BTo delay object creation
CTo delete the object
DTo set initial property values automatically
✗ Incorrect
Constructors help set up initial values so the object is ready to use immediately.
Explain what a constructor method is and why it is useful in PHP classes.
Think about what happens right after you create a new object.
You got /4 concepts.
Write a simple PHP class with a constructor that sets a property called 'name'.
Use __construct and $this->name = ...
You got /4 concepts.
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
Step 1: Understand what a constructor does
A constructor is a special method that runs automatically when an object is created.
Step 2: Identify the purpose of this automatic setup
This setup usually initializes properties or runs code needed for the object to work properly.
Final Answer:
To automatically set up an object when it is created -> Option C
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
Step 1: Recall the special constructor name in PHP
PHP uses the exact name __construct with two underscores at the start.
Step 2: Check each option for correct syntax
Only function __construct() {} uses the correct spelling and underscores for the constructor.
Final Answer:
function __construct() {} -> Option A
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
Step 1: Understand constructor parameter usage
The constructor sets the property color to the value passed ('red').
Step 2: Check what getColor() returns
getColor() returns the color property, which was set to 'red'.
Final Answer:
red -> Option A
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
Step 1: Check constructor syntax
In PHP, the constructor method must have parentheses even if no parameters are used.
Step 2: Identify the missing parentheses
The code misses () after __construct, causing a syntax error.
Final Answer:
Missing parentheses after __construct -> Option D
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
Step 1: Check constructor name and parameters
PHP uses __construct with two parameters $title and $author, matching the requirement.
Step 2: Verify property assignments
The code assigns both $title and $author to object properties using $this correctly.
Final Answer:
class Book {
public $title;
public $author;
public function __construct($title, $author) {
$this->title = $title;
$this->author = $author;
}
} -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.