note 98763 deleted from language.oop5.patterns by danbrown

From: Date: Tue, 06 Jul 2010 13:02:02 +0000
Subject: note 98763 deleted from language.oop5.patterns by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-170733@lists.php.net to get a copy of this message
Note Submitter: pjalva1 at hotmail dot com ---- There is one problem with Quentin Berlemont singleton abstract class. His __construct method should be private. The reason behind why the constructor should be private is explained below in code. <?php //Test Class 1 class TestClass1 extends Singleton { public function Bark() { echo "Bark<br />"; } } //Test Class 2 class TestClass2 extends Singleton { public function Reason() { return new TestClass1; } } //Lets get the instance of TestClass1 $TestClass1 = TestClass1::getInstance(); //And now the second one $TestClass2 = TestClass2::getInstance(); $TestClass2->Reason()->Bark(); //What? We created another instance of TestClass1. That should not be possible in a Singleton class. Why is this? //Well this will error like it should because in the abstract class the constructor is protected. The __construct function is not available to the public. new TestClass1; //Since the constructor is protected the subclasses can access it. Therefore you are allowed to create a new instance of it. Just a slight issue with Quentin Berlemont abstract class and it is a easy fix. ?>

« previous php.notes (#170733) next »