0
0
PHPprogramming~10 mins

Why design patterns matter in PHP - Test Your Understanding

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

Complete the code to create a simple class in PHP.

PHP
<?php
class Car {
    public function [1]() {
        echo "Car started";
    }
}
?>
Drag options to blanks, or click blank then click option'
Arun
Bstart
Cdrive
Dmove
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names that do not describe starting, like 'run' or 'move'.
2fill in blank
medium

Complete the code to implement a singleton pattern method.

PHP
<?php
class Logger {
    private static $instance = null;

    public static function [1]() {
        if (self::$instance === null) {
            self::$instance = new Logger();
        }
        return self::$instance;
    }
}
?>
Drag options to blanks, or click blank then click option'
AnewInstance
BgetLogger
CgetInstance
Dcreate
Attempts:
3 left
💡 Hint
Common Mistakes
Using method names like 'create' or 'newInstance' which do not follow singleton conventions.
3fill in blank
hard

Fix the error in the code to correctly implement dependency injection.

PHP
<?php
class Database {
    private $connection;

    public function __construct([1] $connection) {
        $this->connection = $connection;
    }
}
?>
Drag options to blanks, or click blank then click option'
APDO
Bmysqli
CConnection
DDatabaseConnection
Attempts:
3 left
💡 Hint
Common Mistakes
Using undefined or incorrect class names like 'Connection' or 'DatabaseConnection'.
4fill in blank
hard

Fill both blanks to complete the factory method pattern.

PHP
<?php
interface [1] {
    public function createProduct();
}

class [2] implements ProductFactory {
    public function createProduct() {
        return new Product();
    }
}
?>
Drag options to blanks, or click blank then click option'
AProductFactory
BFactory
CProductCreator
DCreator
Attempts:
3 left
💡 Hint
Common Mistakes
Using the same name for interface and class, causing a PHP name conflict.
5fill in blank
hard

Fill all three blanks to complete the observer pattern example.

PHP
<?php
interface [1] {
    public function update([2] $subject);
}

class [3] implements Observer {
    public function update([2] $subject) {
        echo "Subject state changed.";
    }
}
?>
Drag options to blanks, or click blank then click option'
AObserver
BSubject
CConcreteObserver
DListener
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong interface or class names that do not match the observer pattern.