0
0
PHPprogramming~10 mins

Parent keyword behavior 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 call the parent class method inside the child class.

PHP
<?php
class Base {
    public function greet() {
        echo "Hello from Base\n";
    }
}

class Child extends Base {
    public function greet() {
        [1]::greet();
    }
}

$child = new Child();
$child->greet();
?>
Drag options to blanks, or click blank then click option'
Aself
Bparent
Cstatic
Dthis
Attempts:
3 left
💡 Hint
Common Mistakes
Using self instead of parent to call the parent method.
Using this which refers to the current object, not the parent.
2fill in blank
medium

Complete the code to override the constructor and call the parent constructor.

PHP
<?php
class Animal {
    public function __construct() {
        echo "Animal created\n";
    }
}

class Dog extends Animal {
    public function __construct() {
        [1]::__construct();
        echo "Dog created\n";
    }
}

$dog = new Dog();
?>
Drag options to blanks, or click blank then click option'
Aself
Bthis
Cstatic
Dparent
Attempts:
3 left
💡 Hint
Common Mistakes
Using self::__construct() which calls the current class constructor, causing recursion.
Forgetting to call the parent constructor.
3fill in blank
hard

Fix the error in calling the parent method inside a static method.

PHP
<?php
class ParentClass {
    public static function sayHello() {
        echo "Hello from Parent\n";
    }
}

class ChildClass extends ParentClass {
    public static function sayHello() {
        [1]::sayHello();
        echo "Hello from Child\n";
    }
}

ChildClass::sayHello();
?>
Drag options to blanks, or click blank then click option'
Aself
B$this
Cparent
Dstatic
Attempts:
3 left
💡 Hint
Common Mistakes
Using $this inside a static method causing an error.
Using self which calls the current class method, not the parent.
4fill in blank
hard

Fill both blanks to correctly call the parent method and access a parent property.

PHP
<?php
class Vehicle {
    protected static $type = "Vehicle";
    public function getType() {
        return $this->type;
    }
}

class Car extends Vehicle {
    public function getType() {
        echo [1]::getType();
        echo " is a car.";
    }
    public function showType() {
        echo [2]::$type;
    }
}

$car = new Car();
$car->getType();
$car->showType();
?>
Drag options to blanks, or click blank then click option'
Aparent
Bself
CVehicle
Dthis
Attempts:
3 left
💡 Hint
Common Mistakes
Using self instead of parent to call the parent method.
Using parent to access a non-static property causing an error.
5fill in blank
hard

Fill all three blanks to override a method, call the parent method, and add extra behavior.

PHP
<?php
class Logger {
    public function log($msg) {
        echo "Log: $msg\n";
    }
}

class FileLogger extends Logger {
    public function log($msg) {
        [1]::log($msg);
        echo [2] . " - logged to file" . [3];
    }
}

$logger = new FileLogger();
$logger->log("Error occurred");
?>
Drag options to blanks, or click blank then click option'
Aparent
B$msg
C"\n"
Dself
Attempts:
3 left
💡 Hint
Common Mistakes
Using self instead of parent to call the parent method.
Forgetting to use the variable $msg in the echo statement.
Missing the newline character causing output to be on the same line.