Complete the code to define a constructor method in a PHP class.
<?php class Car { public function [1]() { echo "Car created."; } } ?>
The __construct method is the special function called automatically when a new object is created.
Complete the code to define a destructor method in a PHP class.
<?php class Car { public function [1]() { echo "Car destroyed."; } } ?>
The __destruct method is called automatically when an object is destroyed or script ends.
Fix the error in the constructor method name.
<?php class Bike { public function [1]() { echo "Bike created."; } } ?>
The constructor method must be named __construct with two underscores.
Fill both blanks to create a class with constructor and destructor methods that print messages.
<?php class House { public function [1]() { echo "House built."; } public function [2]() { echo "House demolished."; } } ?>
The constructor is __construct and the destructor is __destruct.
Fill all three blanks to create a class that sets a property in the constructor, prints it, and then prints a message in the destructor.
<?php class Book { public $title; public function [1]($name) { $this->title = $name; } public function printTitle() { echo $this->[2]; } public function [3]() { echo "Book closed."; } } ?>
The constructor is __construct, the property is title, and the destructor is __destruct.