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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T00:16:08+00:00 2026-05-13T00:16:08+00:00

If you had a factory class that creates new objects of some kind, and

  • 0

If you had a factory class that creates new objects of some kind, and that factroy class is a singleton, like this:

class Database_Factory extends Base_Factory {
    private static $factory;
    private $objects = array();

    public function __get($profile) {
        // check for object and return it if it's created before
    }

    public static function getInstance(){
        if (!self::$factory)
            self::$factory = new self();
        return self::$factory;
    }
}

The same code repeats anytime where some object needs it’s own factory. So i decided to make this factory class abstract and implement only specific routines for each factory. But PHP does not allow to instantiate abstract class.

abstract class Base_Factory {
    public static function getInstance(){
        if (!self::$factory)
            self::$factory = new self();
        return self::$factory;
    }
}

Fatal error: Cannot instantiate abstract class Base_Factory

What would you do?

  • 1 1 Answer
  • 1 View
  • 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-13T00:16:08+00:00Added an answer on May 13, 2026 at 12:16 am

    In PHP methods, self always refers to the class where the method is defined. Since version 5.3.0, PHP supports “late static binding”, where you can use the static keyword to access overridden static methods, as well as the function get_called_class() to get the name of the derived class in static context.

    However, your design has a major flaw: The static property $factory defined in Base_Factory is shared across all derived classes. Therefore, the first time a singleton is created and stored in this property, all other calls to getInstance() will return the same object, no matter what derived class is used.

    You could use a static dictionary mapping class names to singleton objects:

    abstract class Base_Factory {
        private static $_instances = array();
        public static function getInstance() {
            $class = get_called_class();
            if (!isset(self::$_instances[$class])) {
                self::$_instances[$class] = new $class();
            }
            return self::$_instances[$class];
        }
    }
    

    Oh, one more thing: The fact that you are looking for a possibility to re-use code for singleton objects could be a cue to the fact that you are over-using the singleton design pattern! Ask yourself if the classes you are planning to implement as singletons really are singletons and if there will be no use case where you might want to have multiple instances of the particular class.

    Often it is much better to use just one singleton representing the current “application context” that provides accessors for objects that are singletons with respect to this context.

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

Sidebar

Related Questions

Assuming you had some kind of factory-created resource that would still belong to the
I have some code that lays out like this: Class1 Task<List<ConfSession>> getSessionsTask = Task.Factory.StartNew(()
I would like to create a singleton class that is instantiated once in each
The below code is a factory class that is delivers objects of type IGraph
I had a debate about macros and their readability. I think that in some
In a recent project I had to create a Singleton class and after a
This is a two part question. I have a class that gets all processes
Due to a recent issue i had some days ago (you can check this
Had a page that was working fine. Only change I made was to add
Had a good search here but can't see anything that gets my mind in

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.