Jump to content

how to prevent a class to be instantiated?


Rommeo
 Share

Go to solution Solved by kicken,

Recommended Posts

Hello,

 

My first question is;

if there is a class, and i dont want this class to be instantiated anywhere? How can i do this? What are the ways/keywords for this?

 

My second question is;

Are there type of classes that can't be instantiated? What are the names of these classes?

 

Thanks in advance.

 

Link to comment
Share on other sites

  • Solution

The info you're looking for would be found by looking up the singleton class pattern. In this design a class is only constructed once by forcing the construction to be done by a static method of the class.

 

You can prevent external code from constructing instances of your class by making your __construct method private.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.