0
0
PHPprogramming~20 mins

Why design patterns matter in PHP - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Design Patterns Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this Singleton pattern example?

Consider this PHP code implementing a Singleton pattern. What will it output?

PHP
<?php
class Singleton {
    private static ?Singleton $instance = null;
    public int $value;

    private function __construct() {
        $this->value = rand(1, 100);
    }

    public static function getInstance(): Singleton {
        if (self::$instance === null) {
            self::$instance = new Singleton();
        }
        return self::$instance;
    }
}

$a = Singleton::getInstance();
$b = Singleton::getInstance();
echo $a->value . ',' . $b->value;
?>
ASame random number printed twice separated by a comma
BTwo different random numbers separated by a comma
CSyntax error due to nullable type declaration
DFatal error: Cannot access private constructor
Attempts:
2 left
💡 Hint

Singleton ensures only one instance exists.

🧠 Conceptual
intermediate
1:30remaining
Why use design patterns in software development?

Which of the following best explains why design patterns matter?

AThey offer proven solutions to common problems, improving code quality and communication
BThey guarantee software will have no bugs
CThey provide ready-made code that can be copied without understanding
DThey force developers to write more complex code to impress others
Attempts:
2 left
💡 Hint

Think about how patterns help teams work together and avoid reinventing the wheel.

🔧 Debug
advanced
2:00remaining
What error does this Factory pattern code produce?

Examine this PHP code snippet using a Factory pattern. What error will it raise when run?

PHP
<?php
interface Product {
    public function getName(): string;
}

class ProductA implements Product {
    public function getName(): string {
        return 'Product A';
    }
}

class ProductFactory {
    public static function create(string $type): Product {
        if ($type === 'A') {
            return new ProductA();
        } elseif ($type === 'B') {
            return new ProductB();
        }
        throw new Exception('Unknown product type');
    }
}

$product = ProductFactory::create('B');
echo $product->getName();
?>
AException: Unknown product type
BFatal error: Class 'ProductB' not found
CSyntax error: missing semicolon
DOutput: Product B
Attempts:
2 left
💡 Hint

Check if all classes used are defined.

📝 Syntax
advanced
2:30remaining
Which option correctly implements the Observer pattern in PHP?

Which of the following PHP code snippets correctly implements the Observer pattern?

A
&lt;?php
interface Observer {
    public function update();
}

class Subject {
    private array $observers = [];

    public function attach(Observer $observer) {
        $this-&gt;observers[] = $observer;
    }

    public function notify() {
        foreach ($this-&gt;observers as $observer) {
            $observer-&gt;update('Hello');
        }
    }
}
?&gt;
B
&lt;?php
class Subject {
    private array $observers;

    public function attach(Observer $observer) {
        $this-&gt;observers-&gt;add($observer);
    }

    public function notify(string $message) {
        foreach ($this-&gt;observers as $observer) {
            $observer-&gt;update($message);
        }
    }
}
?&gt;
C
&lt;?php
interface Observer {
    public function update(string $message);
}

class Subject {
    private array $observers = [];

    public function attach(Observer $observer) {
        $this-&gt;observers[] = $observer;
    }

    public function notify() {
        foreach ($this-&gt;observers as $observer) {
            $observer-&gt;update();
        }
    }
}
?&gt;
D
&lt;?php
interface Observer {
    public function update(string $message);
}

class Subject {
    private array $observers = [];

    public function attach(Observer $observer) {
        $this-&gt;observers[] = $observer;
    }

    public function notify(string $message) {
        foreach ($this-&gt;observers as $observer) {
            $observer-&gt;update($message);
        }
    }
}
?&gt;
Attempts:
2 left
💡 Hint

Check method signatures and how observers are stored and notified.

🚀 Application
expert
3:00remaining
How many objects are created in this Decorator pattern example?

Given this PHP code using the Decorator pattern, how many objects are created in total?

PHP
<?php
interface Coffee {
    public function cost(): float;
}

class SimpleCoffee implements Coffee {
    public function cost(): float {
        return 5.0;
    }
}

class MilkDecorator implements Coffee {
    private Coffee $coffee;

    public function __construct(Coffee $coffee) {
        $this->coffee = $coffee;
    }

    public function cost(): float {
        return $this->coffee->cost() + 1.5;
    }
}

class SugarDecorator implements Coffee {
    private Coffee $coffee;

    public function __construct(Coffee $coffee) {
        $this->coffee = $coffee;
    }

    public function cost(): float {
        return $this->coffee->cost() + 0.5;
    }
}

$coffee = new SugarDecorator(new MilkDecorator(new SimpleCoffee()));
echo $coffee->cost();
?>
A2 objects
B1 object
C3 objects
D4 objects
Attempts:
2 left
💡 Hint

Count each new keyword creating an object.