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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:09:58+00:00 2026-06-15T16:09:58+00:00

If I have: abstract class AbstractSingleton { protected static $instance; public static function &

  • 0

If I have:

abstract class AbstractSingleton
{
    protected static $instance;

    public static function & getInstance()
    {
        if(null === static::$instance) static::$instance = new static();
        return static::$instance;
    }

    protected function __construct() { }
}

Can a user who extends the class redefine the visibility of __construct?

class Singleton extends AbstractSingleton
{
    public function __construct() { } // That would be a problem
}

And second question, what happens for subclasses if __construct is defined as private in AbstractSingleton? Is there not constructor at all? Is a new default constructor provided to the class, and if yes, with what visibility?

  • 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-15T16:10:00+00:00Added an answer on June 15, 2026 at 4:10 pm

    Well, for starters, you don’t need to return a reference (&) from your method. Since PHP 5, objects are passed and returned by reference by default.

    To your actual question, for a child to access a member of a parent, its access modifier needs to be at least protected. That counts for constructors as well. So, in your case, a subclass would be able to do:

    abstract class AbstractSingleton
    {
        protected __ construct() {}
    }
    
    class Singleton extends AbstractSingleton
    {
        public __construct()
        {
            parent::__construct();
    
            // more code
        }
    }
    

    The way to get around that is to use the final keyword. See: PHP documentation.

    For your second question, if you make AbstractSingleton’s constructor private and try to invoke one on its child, then a default, public constructor will be used instead, just like always.

    EDIT: With all that said, Singletons are hardly ever a good idea, especially in PHP. Chances are, you can solve your problem with merely taking advantage of PHP objects automatically being passed/returned by reference. If not, Dependency Injection is a far more OOP friendly alternative.

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

Sidebar

Related Questions

If I have abstract class Parent { static object staticLock = new object(); public
If I have abstract class Base<TSub> { protected static List<TSub> MyStaticList; } class DerivedA
I have public abstract class DataClass { public static bool operator ==(DataClass left, DataClass
I have an abstract class method: Public Class Base_BLL { Public Overridable Function Persist(ByRef
I have abstract class: public abstract ClassA { protected abstract void method1 {...} Another
Suppose we have abstract class A (all examples in C#) public abstract class A
I have abstract BaseController, which basically looks like below: public abstract class BaseController :
I have an abstract class that implements IDisposable, like so: public abstract class ConnectionAccessor
I have an abstract class with a pure virtual function f() and i want
i have Abstract class Public class Abstract baseClass { public abstract string GetString(); public

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.