Skip to content

Add Singleton pattern code example#1041

Open
mhakkou wants to merge 4 commits intocodeguy:gh-pagesfrom
mhakkou:gh-pages
Open

Add Singleton pattern code example#1041
mhakkou wants to merge 4 commits intocodeguy:gh-pagesfrom
mhakkou:gh-pages

Conversation

@mhakkou
Copy link

@mhakkou mhakkou commented Mar 7, 2026

This PR adds a PHP code example for the Singleton design pattern, which was previously marked as TODO: NEED NEW SINGLETON CODE EXAMPLE.
The example includes:

A protected constructor to prevent direct instantiation while allowing subclassing
A private __clone() method to prevent cloning of the instance
A private __wakeup() method to prevent unserializing of the instance
A getInstance() method using late static binding with new static() to support subclassing
Inline comments explaining the purpose of each method
A usage example demonstrating that two calls to getInstance() return the exact same instance

mhakkou and others added 2 commits March 14, 2026 21:32
Co-authored-by: Chris Brown <code@drbyte.dev>
Changed the visibility of the __wakeup method from private to public.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants