A
A
Alexey Nikolaev2014-10-30 05:22:50
PHP
Alexey Nikolaev, 2014-10-30 05:22:50

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

5 answer(s)
A
Alexey Nikolaev, 2016-05-14
@Heian

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.

A
Alexander Sydorenko, 2014-10-30
@San40

Ummm.. A classic singleton cannot be inherited. It has a private constructor. Is not it so? habrahabr.ru/post/31375

D
DancingOnWater, 2014-10-30
@DancingOnWater

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?

If you inherit from a singleton, then you want to. so that these classes are also singletons. Then the answer is obvious - yes.
If you don't want the classes to be singletons, then you don't need to inherit, and then the answer is no.
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.

Sounds like you want something scary. I explain.
Usually in OOP programming, the program is built on classes that have multiple instances. This achieves encapsulation, code reuse, and thread safety. A singleton, on the one hand, is an ordinary class, but having one instance for the entire program.
Too many of them will easily break encapsulation, create problems in multithreading and code reuse, then everything will only get worse.

A
asd111, 2014-10-30
@asd111

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

G
Grigory Vasilkov, 2016-01-27
@gzhegow

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();

And everything was chocolatey and easy. Why, in fact, applications are made on Node, because it is convenient and you don’t have to suffer from hemorrhoids with PHP.
In our case, these 4 lines increase in code of wild sizes. Smoke my last question, I'm just trying to do this right now. Maybe something will come out together.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question