Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6333155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T18:24:21+00:00 2026-05-24T18:24:21+00:00

We recently had a discussion if it was possible to build a trait Singleton

  • 0

We recently had a discussion if it was possible to build a trait Singleton PHP Traits and we played around with it a possible Implementation but ran into issues with building one.

This is an academic exercise. I know that Singletons have very little - if not to say no - use in PHP and that one should 'just create one' but just for exploring the possibilities of traits:

<?php
trait Singleton
{
    protected static $instance;
    final public static function getInstance()
    {
        return isset(static::$instance)
            ? static::$instance
            : static::$instance = new static;
    }
    final private function __construct() {
        static::init();
    }
    protected function init() {}
    final private function __wakeup() {}
    final private function __clone() {}    
}

class A  {
    use Singleton;
    public function __construct() {
        echo "Doesn't work out!";
    }
}

$a = new A(); // Works fine

reproduce: http://codepad.viper-7.com/NmP0nZ

The question is: It is possible to create a Singleton Trait in PHP?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T18:24:21+00:00Added an answer on May 24, 2026 at 6:24 pm

    Quick solution we’ve found (thanks chat!):

    If a trait and a class both define the same method, the one of class if used

    So the Singleton trait only works if the class that uses it doesn’t define a __construct()

    Trait:

    <?php
    trait Singleton
    {
        protected static $instance;
        final public static function getInstance()
        {
            return isset(static::$instance)
                ? static::$instance
                : static::$instance = new static;
        }
        final private function __construct() {
            $this->init();
        }
        protected function init() {}
        final private function __wakeup() {}
        final private function __clone() {}    
    }
    

    Example for a consuming class:

    <?php    
    class A  {
        use Singleton;
    
        protected function init() {
            $this->foo = 1;
            echo "Hi!\n";
        }
    }
    
    var_dump(A::getInstance());
    
    new A();
    

    The var_dump now produces the expected output:

    Hi!
    object(A)#1 (1) {
      ["foo"]=>
      int(1)
    }
    

    and the new fails:

    Fatal error: Call to private A::__construct() from invalid context in ...
    

    Demo

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I recently had to change the linkage specification of several classes and ran into
I recently had a short discussion with another developer about the relationship between PHP's
I recently had a discussion on another forum with another developer and the topic
I recently had a discussion with a colleague about serialization of byte data over
Recently i had a discussion with my boss (a long time C developer) who
I recently had a blog at www.andrewalaniz.com and I have moved it into a
I recently had a discussion with a co-worker on whether we should allow null
Recently I got into a discussion with my Team lead about using temp variables
I have a project that I have recently started working on seriously but had
I recently had to build an app which was pretty big (just under 180mb)

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.