Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

AntMan232's avatar

Illuminate\Container - why isn't singleton updating?

It might be a bit of a niche PHP memory handling question, but I'm quite confused about it!

The codebase I'm working on isn't using Laravel, but we're gradually migrating that direction, and it's currently all running Laravel-style in an Illuminate\Container, extended by an Application implementing the contract as expected.

We don't have Laravel Facades in use (yet), but we do have a very similar class with partial functionality as we move over, which looks a bit like this (stripped down to the relevant methods). We also have a class called SessionHandler which is registered as a singleton in our AppServiceProvider.

final class App {
	public static function get(string $id) {
		return Application::getInstance()->get($id);
	}

	public static function instance(string $abstract, mixed $instance) {
		return Application::getInstance()->instance($abstract, $instance);
	}

	public static function session() {
		return Application::getInstance()->make(SessionHandler::class);
	}
}

What I expected to be able to do was write some code that used App::session(), modified it, and then I expected that because App::session would return a reference to the same singleton instance in memory, the singleton in the container would be updated.

App::session()->setName('newName'); // call some method that changes its internal state
App::session()->getName(); // expected 'newName', received 'oldName'

What I actually seem to have to do, is

$session = App::get(SessionHandler::class);
$session->setName('newName');

// update the singleton instance in the container
App::instance(\ChurchSuite\SessionHandler::class, $session);

Am I fundamentally misunderstanding something?! Thanks for your help!

0 likes
0 replies

Please or to participate in this conversation.