Complete the code to define a constructor in the parent class.
<?php class ParentClass { public function [1]() { echo "Parent constructor called\n"; } } ?>
The constructor method in PHP is named __construct. This special method is called automatically when an object is created.
Complete the code to call the parent constructor from the child class.
<?php class ChildClass extends ParentClass { public function __construct() { [1](); echo "Child constructor called\n"; } } ?>
self::__construct() which calls the current class constructor recursively.this instead of parent.To call the parent class constructor in PHP, use parent::__construct().
Fix the error in the child constructor to properly inherit the parent constructor.
<?php class ChildClass extends ParentClass { public function __construct() { [1]; echo "Child constructor called\n"; } } ?>
-> or dot . instead of double colon.The correct syntax to call the parent constructor is parent::__construct() with parentheses to invoke the method.
Fill both blanks to create a child constructor that calls the parent constructor and sets a property.
<?php class ChildClass extends ParentClass { public $name; public function __construct($name) { [1](); $this->[2] = $name; } } ?>
The child constructor calls the parent constructor with parent::__construct() and sets the property $name using $this->name = $name;.
Fill all three blanks to complete the child constructor that calls the parent constructor with a parameter and sets a property.
<?php class ParentClass { public function __construct($id) { echo "Parent constructor with ID: $id\n"; } } class ChildClass extends ParentClass { public $name; public function __construct($id, $name) { [1]([2]); $this->[3] = $name; } } ?>
The child constructor calls the parent constructor passing $id using parent::__construct($id); and sets the $name property.