Thats weird, because i copied my code directly from this book
http://www.amazon.com/PHP-
Main Topics
Browse All TopicsJust starting out with OO PHP.
I am trying to follow the singleton pattern to set up a base class. This will hold basic information about my site and be subclassed by other modules acting as the link between them all.
I am getting this error:
Fatal error: Using $this when not in object context in ...
It does not like me using $this->config['testing'] = '123';
Here is my code:
class Site {
private static $instance;
public $config = array();
private function __construct() {
// Restrict instantiaton to getInstance()
}
private function __clone() {
// Restrict cloning of object
}
public static function getInstance() {
if (empty(self::$instance)) {
self::$instance = new Site();
// Set up config values
self::initConfig();
}
return self::$instance;
}
private function initConfig() {
$this->config['testing'] = '123';
}
}
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Thats weird, because i copied my code directly from this book
http://www.amazon.com/PHP-
<?php
class Site {
private static $instance;
public $config = array();
private function __construct() {
// Restrict instantiaton to getInstance()
$this->config['testing'] = '123';
}
private function __clone() {
// Restrict cloning of object
}
public static function getInstance() {
if (empty(self::$instance)) {
self::$instance = new Site();
// Set up config values
//self::initConfig();
}
return self::$instance;
}
//private function initConfig() {
// $this->config['testing'] = '123';
//}
}
$x = Site::getInstance();
print_r($x);
?>
Try that...
A singleton exists once. A theoretical subclass of singleton is still a singleton and therefore you can't create a second instance.
I use a singletonDBFactory to create singletonDBConnections. I connect to many different DB types (MSSQL, mySQL, MS Access, MS Excel, Sage Retrieve 4GL, etc), but I don't want to connect more than once to any of them. And as they are all the same, I have a factory to create them for me and I only want one of them also.
But I cheat on the connectors. These are all owned by the factory and as such I can determine if I've already connected.
Mix and match.
But extending singletons isn't right.
As RQuadling mentions, a singleton is just that a class resticted to a single instance, and an instance represents an object of that class. Extending such a thing is a strange way of doing things.
If you want to represent a central point for accessing a variety of data look at the registry pattern. The registry itself is often a singleton but it will contain arrays of data. A factory pattern (actually factory method to be exact) can be used to produce these instances.
If you want prime examples of this in action take a look at Joomla, and download their code - these people are really clever
\joomla\libraries\joomla\f
\joomla\libraries\joomla\r
and if you are wondering how they do this with PHP4 compatible code take a look at the 'base' class.
Business Accounts
Answer for Membership
by: RQuadlingPosted on 2007-09-19 at 06:58:52ID: 19920644
Take a look at the following user notes on http://www.php.net/manual/ en/languag e.oop5.pat terns.php
en/ languag e.oop5.pat terns.php# 53483 en/ languag e.oop5.pat terns.php# 52027 en/ languag e.oop5.pat terns.php# 52021 en/ languag e.oop5.pat terns.php# 52003
http://www.php.net/manual/
http://www.php.net/manual/
http://www.php.net/manual/
http://www.php.net/manual/
etc.
Basically, you can't make a subclass a singleton to be extended as only the base class's constructor can be called and that has to be private and static to stop the manual creation by new.
In your code, $this should still be self as you are dealing with it statically.