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

  • SEARCH
  • Home
  • 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 8828435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T07:36:21+00:00 2026-06-14T07:36:21+00:00

I’m new to this level of PHP programming and I’ve been reading a post

  • 0

I’m new to this level of PHP programming and I’ve been reading a post about singleton and static classes. I’m in the process of writing a class that would facilitate my DB connections.

I came across the following code by Jon Raphaelson (here) :

class ConnectionFactory
{
    private static $factory;
    public static function getFactory()
    {
        if (!self::$factory)
            self::$factory = new ConnectionFactory(...);
        return self::$factory;
    }

    private $db;

    public function getConnection() {
        if (!$this->db)                 // this line was modified due to comment
            $this->db = new PDO(...);       // this line was modified due to comment
        return $db;
    }
}

function getSomething()
{
    $conn = ConnectionFactory::getFactory()->getConnection();
    .
    .
    .
}

It seemed like I had found what I was looking for, however I have a few questions about it.

  1. self::$factory = new ConnectionFactory(...); – I don’t see a constructor in this class. Do I just create this constructor and pass in the db details (‘dbname’, ‘user’, ‘pass’, etc)?
  2. the getSomething() function, I’m assuming that the intent was to put all of the actual functions that would retrieve data within the ConnectionFactory class – and this is the reason for this function being in this class. Otherwise, I would have expected this function to be within another class. [edit] SKIP THIS QUESTION, I didn’t see the one bracket.
  3. What happens when two users are logged into the site and are requesting the DB connection (both are doing updates, etc)? Will it be an issue that this is a singleton?

Thanks!

  • 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-06-14T07:36:22+00:00Added an answer on June 14, 2026 at 7:36 am

    This is mostly to expand on my comments under the question…

    The better “singleton” pattern would be this:

    class ConnectionFactory {
    
        protected static $connection;
    
        public function getConnection() {
            if (!self::$connection) {
                self::$connection = new PDO(...);
            }
            return self::$connection;
        }
    
    }
    

    The users of this factory should expect an instance of it, not call it themselves:

    class Foo {
    
        protected $connectionFactory;
    
        public function __construct(ConnectionFactory $factory) {
            $this->connectionFactory = $factory;
        }
    
        public function somethingThatNeedsAConnection() {
            $connection = $this->connectionFactory->getConnection();
            ...
        }
    
    }
    
    $foo = new Foo(new ConnectionFactory);
    

    This allows Foo to get a connection itself when needed, but also allows you to inject some alternative connection into Foo, for example for testing purposes.

    As a convenience measure and to cut down on instantiation complexity, this pattern is also good:

    class Foo {
    
        protected $connectionFactory;
    
        public function __construct(ConnectionFactory $factory = null) {
            if (!$factory) {
                $factory = new ConnectionFactory;
            }
            $this->connectionFactory = $factory;
        }
    
        ...
    
    }
    

    This still allows the dependency to be injected and overridden, but it doesn’t require you to inject a factory every time you instantiate Foo.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but

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.