Unraveling the Domain-System: Boot, Singleton, and Kernel: Part 1

Unraveling the Domain-System: Boot, Singleton, and Kernel: Part 1

Leader ●3 ●8
calendar_today ago • schedule3 min read

Understanding the Domain-System


link: https://github.com/hadelrachid/domain-system


From the first execution to the birth of the Kernel

  1. After all, what is Boot?

    What happens when someone accesses a system for the first time? Who starts everything?

  2. What is an object-oriented application?

    Classes, objects, methods, properties, and responsibilities.

  3. The problem of creating several identical objects

    Why can this be inconvenient in certain components?

  4. Getting to know Singleton

    The idea of a single instance, how it works, and its limitations.

  5. Singleton in four languages

    Python, Java, C#, and PHP: same idea, different syntaxes and particularities.

  6. The Domain-System Bootstrap

    How the entry point prepares the environment and starts the application.

  7. The birth of the Kernel

    How the application centralizes services and prepares the system to work.

  8. The first request

    What happens after boot finishes and an HTTP request arrives?

This roadmap is a proposal for didactic organization. We will check each step against the real code, without attributing behaviors to the project that are not proven.

1. First, we need to understand what happens in the Domain-System

Imagine you have just installed the Domain-System on a server.

A visitor accesses:

https://yourdomain.com

The browser makes an HTTP request. The server needs to find an entry point to start executing PHP.

In the project, this point is:

public/index.php

This file is known as the Front Controller: the main gateway into the application.

The flow identified in the current code is:

1. Browser
HTTP request
↓
2. public/index.php
Front Controller: checks for maintenance and starts the flow
↓
3. bootstrap.php
Prepares the environment and creates the fundamental services
↓
4. Application
Instantiates the Kernel and prepares its services
↓
5. $app->boot()
Initializes sessions, discovers plugins, and dispatches events
↓
6. System initialized
Request processing can continue


This flow is documented and implemented in the files public/index.php, bootstrap.php, and src/Core/Application.php.

An important observation: creating the Kernel and initializing the Kernel are two different steps.

  • Create: new Application(...).
  • Initialize: $app->boot().

This distinction will be one of the first lessons of the tutorial.

2. How are we going to teach Singleton?

I don't want to start by presenting a huge PHP class with static properties, constructors, and special methods.

First, let's present an everyday problem.

Imagine a school that has a single central enrollment control system.

Several employees need to consult this system. We want everyone to work with the same instance, not for each employee to create an independent central system.

This is the fundamental idea of Singleton:

A design pattern that seeks to guarantee a single instance of a given class and provide a point of access to it.

The pattern was formalized in the book Design Patterns: Elements of Reusable Object-Oriented Software, published in 1994 by the four authors known as the Gang of Four (GoF). Its classic problem is precisely to guarantee a single instance and a global point of access.

The didactic evolution across languages

We will use the same example in all of them. To avoid the conflict with the System namespace in C#, we will call the example class EnrollmentSystem.

1. Python — the gateway

Let's start with a simple implementation, using the language's resources and explaining each line.

class EnrollmentSystem:
    _instance = None

    @classmethod
    def get_instance(cls):
        if cls._instance is None:
            cls._instance = cls()
        return cls._instance

system1 = EnrollmentSystem.get_instance()
system2 = EnrollmentSystem.get_instance()

print(system1 is system2)  # True

2. Java — understanding the mechanisms

Here we will study the private constructor, static attribute, and static method.

public class EnrollmentSystem {
    private static EnrollmentSystem instance;

    private EnrollmentSystem() {
    }

    public static EnrollmentSystem getInstance() {
        if (instance == null) {
            instance = new EnrollmentSystem();
        }

        return instance;
    }
}

3. C# — comparing with Java

We will notice the similarities and differences in syntax and explore static properties.

public class EnrollmentSystem
{
    private static EnrollmentSystem? instance;

    private EnrollmentSystem()
    {
    }

    public static EnrollmentSystem GetInstance()
    {
        if (instance == null)
        {
            instance = new EnrollmentSystem();
        }

        return instance;
    }
}

4. PHP — arriving at the Domain-System

We will understand static properties, constructors, static methods, types, and visibility.

<?php

class EnrollmentSystem
{
    private static ?EnrollmentSystem $instance = null;

    private function __construct()
    {
    }

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

        return self::$instance;
    }
}

$system1 = EnrollmentSystem::getInstance();
$system2 = EnrollmentSystem::getInstance();

var_dump($system1 === $system2); // true

Didactic note: these are introductory Singleton examples, not complete production-ready implementations. We will also study concurrency, safe initialization, and the differences between the languages. Python, Java, C#, and PHP do not have exactly the same mechanisms.

3. An important discovery in the real code

When examining src/Core/Application.php, I found a particularity that deserves attention in the tutorial.

The class declares:

private static ?Application $instance = null;

But its constructor is public:

public function __construct(
    ContainerInterface $container,
    EventDispatcherInterface $dispatcher,
    string $basePath
)

And inside it:

self::$instance = $this;

This means that the implementation keeps a static reference to the created instance. However, since the constructor is public, another part of the code can also execute new Application(...).

Therefore, we need to teach an important distinction:

The project uses a centralized instance through the initialization flow, but the class, in isolation, does not technically prevent the creation of other instances.

There are also comments stating that clone and unserialize are blocked, but I did not find the corresponding protection methods in the examined implementation. We should not teach that this shielding is effectively guaranteed without correcting or verifying this point.

This is excellent didactic material: it teaches the student not to confuse what a comment promises with what the code actually guarantees.

4. What else will we study about Singleton?

We will not limit the subject to the basic implementation.

Concept What the student will understand
static What belongs to the class, not to each object
Constructor How an object is born
private and public Who can access and create objects
Lazy Singleton Create only when necessary
Eager Singleton Create in advance
self and static How to access class members in PHP
clone How to copy objects
Serialization How to transform objects into data and recover them
DI Container How to distribute objects without relying on global access
Limitations Testability, coupling, and global state

And there is an especially important question for the Domain-System: the PHP Singleton does not mean a permanent instance shared by all site visitors. In a traditional PHP application, the static instance exists during the request execution cycle. A new request normally starts a new execution cycle.

5. How to turn this into a series on the Domain-System?

I would organize the articles like this:

  1. The birth of an application: what is Boot?
  2. Before Singleton: classes, objects, and instances.
  3. The problem of multiple instances.
  4. Singleton explained with Python.
  5. Singleton explained with Java.
  6. Singleton explained with C#.
  7. Singleton explained with PHP.
  8. Eager Loading and Lazy Loading.
  9. The Domain-System Singleton.
  10. The bootstrap.php file, line by line.
  11. Application.php: the birth of the Kernel.
  12. The boot() method: when the system wakes up.
  13. DI Container: how services are distributed.
  14. The complete cycle of an HTTP request.

Our tutorial Understanding SOLID Architecture already has material that can serve as a basis for this series to aid understanding!

Let's reason?

"When you type a website address into the browser, who wakes up the system to serve you?"

From this question, we present the concept of Boot, the analogy with starting a computer, and finally, public/index.php and bootstrap.php.

Only afterward do we introduce Singleton!

This way, when the programmer encounters Application::getInstance(), they will already have understood why the system needs a centralized instance.

I will write one article at a time, with human-friendly explanation, executable examples, and diagrams.


Want more #python & #php deep dives?

Join 6,101+ developers sharing real benchmarks, code breakdowns, and projects. Free forever.

Part 1 of 1 in Part 1

More Posts

Attention-Free Score: How Domain Reports Show Which Pages Need Work

ApogeeWatcherverified - Sep 1

Dashboard Operasional Armada Rental Mobil dengan Python + FastAPI

Masbadar - Mar 12

Cloudflare Blocked My Server, So I Made My Visitors' Browsers Do the Fetching

bryanhamiltondevverified - Nov 15

AI Email Classifier Cost per 1,000 Messages: Real Math

floxolab - Oct 8

When One Python Process Fails, You Need to See All of Them

Tom Smithverified - Nov 23
chevron_left
796 Points • 11 Badges
3Posts
2Comments
5Connections
I am a self-taught student of software architecture. I am interested in programming languages ​​such... Show more

Related Jobs

View all jobs →

Commenters (This Week)

1 comment
1 comment
1 comment

Contribute meaningful comments to climb the leaderboard and earn badges!