0
0
PHPprogramming~20 mins

Singleton pattern in PHP - Practice Problems & Coding Challenges

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

Consider this PHP Singleton class. What will be the output when running the code below?

PHP
<?php
class Logger {
    private static ?Logger $instance = null;
    private function __construct() {}
    public static function getInstance(): Logger {
        if (self::$instance === null) {
            self::$instance = new Logger();
        }
        return self::$instance;
    }
    public function log(string $msg): void {
        echo $msg . "\n";
    }
}

$log1 = Logger::getInstance();
$log2 = Logger::getInstance();
if ($log1 === $log2) {
    echo "Same instance";
} else {
    echo "Different instances";
}
?>
ASame instance
BDifferent instances
CFatal error: Cannot instantiate private constructor
DSyntax error
Attempts:
2 left
💡 Hint

Think about how the Singleton pattern ensures only one instance exists.

Predict Output
intermediate
2:00remaining
What error does this Singleton code produce?

What error will this PHP code produce when executed?

PHP
<?php
class Config {
    private static ?Config $instance = null;
    private function __construct() {}
}

$config = new Config();
?>
AWarning: Undefined property
BNo error, object created successfully
CParse error: syntax error
DFatal error: Cannot instantiate private constructor
Attempts:
2 left
💡 Hint

Check the constructor visibility and how the object is created.

🧠 Conceptual
advanced
1:30remaining
Why use a private constructor in Singleton pattern?

Why is the constructor declared private in a Singleton class?

ATo prevent creating multiple instances from outside the class
BTo make the class abstract
CTo enable multiple instances to be created internally
DTo allow subclassing of the Singleton class
Attempts:
2 left
💡 Hint

Think about how Singleton controls instance creation.

Predict Output
advanced
2:00remaining
What is the output of this modified Singleton code?

What will this PHP code output?

PHP
<?php
class Counter {
    private static ?Counter $instance = null;
    private int $count = 0;
    private function __construct() {}
    public static function getInstance(): Counter {
        if (self::$instance === null) {
            self::$instance = new Counter();
        }
        return self::$instance;
    }
    public function increment(): void {
        $this->count++;
    }
    public function getCount(): int {
        return $this->count;
    }
}

$c1 = Counter::getInstance();
$c1->increment();
$c1->increment();
$c2 = Counter::getInstance();
$c2->increment();
echo $c2->getCount();
?>
A1
B2
C3
D0
Attempts:
2 left
💡 Hint

Remember that both variables point to the same instance.

🔧 Debug
expert
2:30remaining
Why does this Singleton implementation fail to enforce single instance?

Examine this PHP Singleton code. Why does it fail to enforce a single instance?

PHP
<?php
class BrokenSingleton {
    public static ?BrokenSingleton $instance = null;
    public function __construct() {}
    public static function getInstance(): BrokenSingleton {
        if (self::$instance === null) {
            self::$instance = new BrokenSingleton();
        }
        return self::$instance;
    }
}

$a = new BrokenSingleton();
$b = BrokenSingleton::getInstance();
if ($a === $b) {
    echo "Same instance";
} else {
    echo "Different instances";
}
?>
AStatic property is not initialized properly
BConstructor is public, so external code can create multiple instances
CgetInstance() method is missing return statement
DClass is missing a destructor
Attempts:
2 left
💡 Hint

Check the constructor's visibility and how instances are created.