Answer the question
In order to leave comments, you need to log in
Should subclass constructors be private when implementing a singleton?
Good time!
There is a classic singleton class. Other classes inherit from it. Question: should these classes implement the logic of the pattern in the same way (i.e. through getInstance), or can they just be created in the classical way through new? Is there any guidance on this, or can the pattern be used as needed even in subclasses ?
Alternatively, is it correct and good from the point of view of the purity and beauty of programming to create instance-type variables in the parent class:
...and then create them in the inherited class:
To thus have instances of subclasses concentrated in a single main class?
I would be grateful for advice and recommendations, thanks. public static $_controller;
parent::$_controller = new self;
Answer the question
In order to leave comments, you need to log in
As it turned out a year later, it was a kind of Dependency Injection, and in fact the question had nothing to do with the singleton.
Ummm.. A classic singleton cannot be inherited. It has a private constructor. Is not it so? habrahabr.ru/post/31375
There is a classic singleton class. Other classes inherit from it. Question: should these classes implement the logic of the pattern in the same way (i.e. through getInstance), or can they just be created in the classical way through new?
Alternatively, is it correct and good from the point of view of purity and beauty of programming to create instance-type variables in the parent class:
public static $_controller;
...and after that, create them in the inherited class:
parent::$_controller = new self;
To thus have instances of subclasses concentrated in a single main class?
I would be grateful for advice and recommendations, thanks.
In my opinion, in your case it is better to make a protected static controller so that it is inaccessible to non-heirs and write self::controller=new self() then your parent will be like an interface and the heirs will implement it
I think I understand what you mean. Right now I'm digging into this myself.
You want to make micro-modules that are controlled from a single shell/bus/module, right?
In JS, this was done very clearly:
App = {};
Module = {
b : function() {}
};
App.b = Module.b;
App.b();
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question