0
0
PHPprogramming~20 mins

Observer pattern in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Observer Pattern Master
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 Observer pattern example?

Consider this PHP code implementing a simple observer pattern. What will it print?

PHP
<?php
interface Observer {
    public function update(string $message): void;
}

class Subject {
    private array $observers = [];

    public function attach(Observer $observer): void {
        $this->observers[] = $observer;
    }

    public function notify(string $message): void {
        foreach ($this->observers as $observer) {
            $observer->update($message);
        }
    }
}

class ConcreteObserver implements Observer {
    private string $name;

    public function __construct(string $name) {
        $this->name = $name;
    }

    public function update(string $message): void {
        echo $this->name . " received: " . $message . "\n";
    }
}

$subject = new Subject();
$observer1 = new ConcreteObserver("Observer1");
$observer2 = new ConcreteObserver("Observer2");
$subject->attach($observer1);
$subject->attach($observer2);
$subject->notify("Hello Observers!");
?>
ANo output
B
Observer1 received: Hello Observers!
Observer2 received: Hello Observers!
C
Observer1 received: Hello Observers!
D
Observer2 received: Hello Observers!
Attempts:
2 left
💡 Hint

Think about how many observers are attached and what notify() does.

🧠 Conceptual
intermediate
1:30remaining
What is the main purpose of the Observer pattern?

Choose the best description of the Observer pattern's main purpose.

ATo encapsulate a group of individual algorithms.
BTo restrict access to certain parts of an object.
CTo allow an object to notify other objects automatically when its state changes.
DTo create a single instance of a class throughout the application.
Attempts:
2 left
💡 Hint

Think about how objects communicate changes in state.

🔧 Debug
advanced
2:00remaining
Why does this Observer pattern code cause a fatal error?

Look at this PHP code snippet. It causes a fatal error. What is the cause?

PHP
<?php
interface Observer {
    public function update(string $message): void;
}

class Subject {
    private array $observers = [];

    public function attach(Observer $observer): void {
        $this->observers[] = $observer;
    }

    public function notify(string $message): void {
        foreach ($this->observers as $observer) {
            $observer->update($message);
        }
    }
}

class ConcreteObserver {
    private string $name;

    public function __construct(string $name) {
        $this->name = $name;
    }

    public function update(string $message): void {
        echo $this->name . " received: " . $message . "\n";
    }
}

$subject = new Subject();
$observer = new ConcreteObserver("Obs1");
$subject->attach($observer);
$subject->notify("Test");
?>
AFatal error because ConcreteObserver does not implement Observer interface.
BFatal error because attach() method is missing.
CFatal error because update() method has wrong parameter type.
DNo fatal error, code runs fine.
Attempts:
2 left
💡 Hint

Check if the observer class matches the interface type hint.

📝 Syntax
advanced
2:30remaining
Which option correctly implements the detach method in Subject?

We want to add a detach method to remove an observer from the Subject's list. Which option correctly implements this?

PHP
<?php
class Subject {
    private array $observers = [];

    public function attach(Observer $observer): void {
        $this->observers[] = $observer;
    }

    // Implement detach method here
}
?>
A
public function detach(Observer $observer): void {
    $index = array_search($observer, $this-&gt;observers, true);
    if ($index !== false) {
        unset($this-&gt;observers[$index]);
    }
}
B
public function detach(Observer $observer): void {
    foreach ($this-&gt;observers as $key =&gt; $obs) {
        if ($obs == $observer) {
            unset($this-&gt;observers[$key]);
        }
    }
}
C
public function detach(Observer $observer): void {
    $this-&gt;observers = array_filter($this-&gt;observers, fn($obs) =&gt; $obs !== $observer);
}
D
public function detach(Observer $observer): void {
    $this-&gt;observers = [];
}
Attempts:
2 left
💡 Hint

Use strict comparison and remove only the matching observer.

🚀 Application
expert
3:00remaining
How many observers will be notified after this code runs?

Given this PHP code, how many observers will receive the notification?

PHP
<?php
interface Observer {
    public function update(string $msg): void;
}

class Subject {
    private array $observers = [];

    public function attach(Observer $observer): void {
        $this->observers[] = $observer;
    }

    public function detach(Observer $observer): void {
        $index = array_search($observer, $this->observers, true);
        if ($index !== false) {
            unset($this->observers[$index]);
        }
    }

    public function notify(string $msg): void {
        foreach ($this->observers as $observer) {
            $observer->update($msg);
        }
    }
}

class ConcreteObserver implements Observer {
    private string $id;
    public function __construct(string $id) {
        $this->id = $id;
    }
    public function update(string $msg): void {
        echo "Observer {$this->id} got: {$msg}\n";
    }
}

$subject = new Subject();
$obs1 = new ConcreteObserver("1");
$obs2 = new ConcreteObserver("2");
$obs3 = new ConcreteObserver("3");

$subject->attach($obs1);
$subject->attach($obs2);
$subject->attach($obs3);
$subject->detach($obs2);
$subject->attach($obs2);
$subject->detach($obs1);
$subject->notify("Update!");
?>
A1
B0
C3
D2
Attempts:
2 left
💡 Hint

Track attach and detach calls carefully.