note 98763 added to language.oop5.patterns

From: Date: Tue, 06 Jul 2010 03:19:13 +0000
Subject: note 98763 added to language.oop5.patterns
Groups: php.notes 
Request: Send a blank email to php-notes+get-170725@lists.php.net to get a copy of this message
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. ?> ---- Server IP: 192.41.42.26 Probable Submitter: 69.129.226.57 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.patterns.php Edit -- https://master.php.net/note/edit/98763 Del: integrated -- https://master.php.net/note/delete/98763/integrated Del: useless -- https://master.php.net/note/delete/98763/useless Del: bad code -- https://master.php.net/note/delete/98763/bad+code Del: spam -- https://master.php.net/note/delete/98763/spam Del: non-english -- https://master.php.net/note/delete/98763/non-english Del: in docs -- https://master.php.net/note/delete/98763/in+docs Del: other reasons-- https://master.php.net/note/delete/98763 Reject -- https://master.php.net/note/reject/98763 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#170725) next »