Jump to content

Recommended Posts

so i am open to suggestions and comments

 

<?php
if(!defined('_ROOT'))
exit(header('HTTP/1.0 404 Not Found'));

class configuration {

private static $_instance;

private configuration(){
}

public static function &getInstance(){
	if(!self::$_instance)
		self::$_instance = new configuration();
	return self::$_instance;
}

}

$config = configuration::getInstance();

?>

 

Link to comment
https://forums.phpfreaks.com/topic/250093-creating-an-oop-class/
Share on other sites

If you're using PHP 5+, you don't need the '&' in front of getInstance().

 

Aside from that, your class is a bare bones, textbook example/copy of a Singleton.  There's nothing to critique or say, aside from the general warning about using Singletons.

 

ty for the hint about '&' as i am using php5.3.8

 

and what do you mean by "general warning about using singletons" ?

Singletons are just another global variable.  Globals are bad, as they break scope and encapsulation, both of which are cornerstones of good programming in general, and OOP specifically.  Usually, if you're thinking about using a Singleton, it's a sign that you haven't thought about your design properly.  Not always, but usually.  A lot of "Should I use a Singleton here?" questions can be solved much more eloquently by using Dependency Injection/Inversion of Control (which are two names for the same thing).  Look into the Symfony Dependency Injection Container (Google it) for a general idea of what it is.

I don't think I can agree that a Singleton class is equivalent to global variables. Sure they certainly can be overused, but everything can.

There are plenty problems that are perfectly solved by a Singleton class. (like when pooling database connections or threads)

The situations that call for a Singleton, I'd guess, should only occur once or twice in a typical application, IF at all.

 

for the record, I think global variables are much more offensive that Singletons.

The situations that call for a Singleton, I'd guess, should only occur once or twice in a typical application, IF at all.

 

Usually' date=' if you're thinking about using a Singleton, it's a sign that you haven't thought about your design properly.  Not always, but usually.[/quote']

 

I don't see much difference between these two statements. 

 

Also note that the vast, vast majority of members asking for OOP help are complete newbies.  They're likely not going to be concerned about pooling database connections (and PHP doesn't have threads anyway, so that's a moot example). 

 

In my experience, beginners often attempt to use what few design patterns they successfully learn as the solution to all problems, at times even going so far as to think that an app isn't well designed unless it features at least one example of each of those patterns.  I was attempting to curb that, and the potential misuse of a pattern which remains controversial specifically because it is misused so often.

 

Singletons, as you noted, have their place.  However, they should be considered the last chosen, least appealing alternative if only because they break encapsulation by their very nature.

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

×
×
  • 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.