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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T02:34:59+00:00 2026-05-14T02:34:59+00:00

When creating a Singleton in PHP, I ensure that it cannot be instantiated by

  • 0

When creating a Singleton in PHP, I ensure that it cannot be instantiated by doing the following:

class Singleton {

    private function __construct() {}
    private function __clone() {}

    public static function getInstance() {}
}

However, I realised that defining a class as ‘abstract’ means that it cannot be instantiated. So is there anything wrong with doing the following instead:

abstract class Singleton {

    public static function getInstance() {}
}

The second scenario allows me to write fewer lines of code which would be nice. (Not that it actually makes much of a difference.)

  • 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-14T02:34:59+00:00Added an answer on May 14, 2026 at 2:34 am

    When creating a singleton in PHP, declaring the __construct and __clone as private ensures that the class cannot be instanciated from the outside : it can still be instanciated from inside its declaration.

    When declaring a class as abstract, it can not be instanciated at all ; not even from inside its declaration.

    This means your solution would not work : in the second case, your getInstance() method will not be able to instanciate the class — while it can do so in the first case.

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

Sidebar

Related Questions

I have always used a Singleton class for a registry object in PHP. As
The following code is from a tutorial ( http://net.tutsplus.com/php/creating-a-php5-framework-part-1/ ), not mine. I have
Creating a simple public/private keypair, such as through ssh-keygen does not create a file
While creating classes in Java I often find myself creating instance-level collections that I
Greetings, I would like to ask if creating Singleton to have only one active
When would a singleton actually be easier or better than a static class? It
I wish to add a singleton method to a particular object. I wish that
I have an object, based on the Singleton design, that I use for user
I'm calling my custom factory that I created (PhotoServiceFactory), which is a singleton that
Creating a patch is very easy in SubVersion, With Tortoise, you right-click and select

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.