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 7618183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T03:20:21+00:00 2026-05-31T03:20:21+00:00

class ConnectionFactory { private static $factory; public static function getFactory() { if (!self::$factory) self::$factory

  • 0
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 (!$db)
            $db = new PDO(...);
        return $db;
    }
}

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

Source

There are a couple of things that i dont get

  1. Am i right when i say “staic property of a class can be accessed without initiating the class”
  2. what does !db do
  3. How is this happening ConnectionFactory::getFactory()->getConnection();
  4. Could somebody explain getFactory method
  • 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-31T03:20:22+00:00Added an answer on May 31, 2026 at 3:20 am
    1. you are right here.
    2. ! is a NOT. Which means here that if $db is false then initialise it. Since its in a static method it will stay initialised and next time the existiong $db will be returned since this second time !$db == false.
    3. like for $db it is checking if an instance of $factory exists, if not create one and returne it, otherwise return the existing one.

    4.

    public static function getFactory()
    {
         if (!self::$factory) // is self::$factory initialised?
                self::$factory = new ConnectionFactory(...); //initialise it
         return self::$factory; //return self::$factory
    }
    

    Also here $factory seems to be a variable that is set somewhere eles. Presumably it could contain a couple of different class names. Does not change anything to the way the function works. Its a singleton pattern

    Adding a interesting link about this pattern wikipedia

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

Sidebar

Related Questions

class B{ private: void DoSomething(); } class W{ private: class D: public B{ }
OK some background. I have something similar to this: class ConnectionFactory { public IConnection
class Ef_AppSecurity extends Zend_Controller_Plugin_Abstract { public function preDispatch(Zend_Controller_Request_Abstract $request) { if (!Zend_Auth::getInstance()->getIdentity()) { $redirect
class a { public $test=msg1; } $t1 = new a; echo echo1: After Instantiation
class A(object): def get_class(self): return self.__class__ class B(A): def __init__(self): A.__init__(self) b = B()
class sample { private: int radius; float x,y; public: circle() { } circle(int rr;float
class test <T> where T : class { public void Write<T>() { Console.Write(typeof(T).FullName); }
class foo { public: void say_type_name() { std::cout << typeid(this).name() << std::endl; } };
class Hello @hello = hello def display puts @hello end end h = Hello.new
Class someInterface = Class.fromName(some.package.SomeInterface); How do I now create a new class that implements

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.