Construct() would only be called if you called it from within a static method for the class containing the private constructor. So for your Singleton, you might have a method like so: class DBConnection { private static $Connection = null; public static function getConnection() { if(!isset(DBConnection::Connection)) { DBConnection::Connection = new DBConnection(); } return DBConnection::Connection; } private function __construct() { } } $dbConnection = DBConnection::getConnection() The reason you are able/would want to instantiate the class from within itself is so that you can check to make sure that only one instance exists at any given time. This is the whole point of a Singleton, after all.
Using a Singleton for a database connection ensures that your application is not making a ton of DB connections at a time.
Construct() would only be called if you called it from within a static method for the class containing the private constructor. So for your Singleton, you might have a method like so: class DBConnection { private static $Connection = null; public static function getConnection() { if(!isset(DBConnection::Connection)) { DBConnection::Connection = new DBConnection(); } return DBConnection::Connection; } private function __construct() { } } $dbConnection = DBConnection::getConnection(); The reason you are able/would want to instantiate the class from within itself is so that you can check to make sure that only one instance exists at any given time. This is the whole point of a Singleton, after all.
Using a Singleton for a database connection ensures that your application is not making a ton of DB connections at a time.
Test the code, Mark, and let me know what you find out. EDIT: Also, in this particular situation, I'm not sure why you would need to be concerned with preventing a person from subclassing. If someone has access to your PHP code to subclass it, then they also have access to your code to copy it and change access modifiers to something that they (for whatever reason) find suitable.
The practical usefulness of the Singleton in this case is that, by using it, you can ensure that you're always using the same database connection for a given HTTP request. It accomplishes that. The other stuff (using final and private constructors) is useful to know from a theory perspective, and even more useful to know if you want to distribute API-quality code to other programmers, but in the case of this particular example, all the keywords are doing is adding bytes to your class file's size.
OK, I ran some tests of my own. If you don't declare your own constructor in the subclass, trying to instantiate it will throw a fatal error, because it tries to call the superclass constructor. In the case of a DB connection, when you're presumably returning (in PHP, anyway) a mysqli instance or the resource returned by the mysql_connect() function (or some other link to some other RDBMS), as long as you mark the instance private, there's no threat of somebody subclassing it and tampering with the link.As I alluded to before, if somebody really wants to override your behavior and make multiple connections, they could just as well do it by writing a new class.
A little nit picking: For completeness you'd probably declare the class as final too since you wouldn't want someone subclassing this class and implementing its own public constructor. (Forgive me if the compiler catches the overriding of a private constructor, but I don't think it does).
Here's a very simple singleton that just generates a date/time string: class TheDate { private static $DateInstance = null; private $dateVal; public static function getDateInstance() { if(!isset(TheDate::$DateInstance)) { TheDate::$DateInstance = new TheDate(); } return TheDate::$DateInstance; } public static function getDateVal() { return TheDate::$DateInstance->dateVal; } private function __construct() { $this->dateVal = strftime("%Y-%m-%d %H:%M:%S"); } } Doing something like this obviously gives me the same date over and over: $date1 = TheDate::getDateInstance(); echo $date1->getDateVal() . ""; $date2 = TheDate::getDateInstance(); echo $date2->getDateVal() . ""; And doing this doesn't generate any errors: class NewDate extends TheDate { public function __construct() { } }.
EDIT: Also, in this particular situation, I'm not sure why you would need to be concerned with preventing a person from subclassing. If someone has access to your PHP code to subclass it, then they also have access to your code to copy it and change access modifiers to something that they (for whatever reason) find suitable. The practical usefulness of the Singleton in this case is that, by using it, you can ensure that you're always using the same database connection for a given HTTP request.
It accomplishes that. The other stuff (using final and private constructors) is useful to know from a theory perspective, and even more useful to know if you want to distribute API-quality code to other programmers, but in the case of this particular example, all the keywords are doing is adding bytes to your class file's size.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.