473,725 Members | 2,272 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Singleton class scope problem

On my site I want to make two classes. One that can be instantiated
normally and one with extended functionality that can only be
instantiated once. The Singleton pattern thus seems like a logical
choice for the second class. However, I would like the second
(singleton) class to extend the first. Something like this:
<?php
class User
{
public function __construct ()
{
doSomething();
}
}

class Member extends User
{
private static $instance = NULL;
public static function getInstance ()
{
if (self::$instanc e === NULL)
self::$instance = new Member;
return self::$instance ;
}

private function __construct () {}
private function __clone () {}
}
?>
But this gives me the following error:
Fatal error: Access level to Member::__const ruct() must be public (as
in class User) in class.member.ph p on line 6

But I don't want the constructor to be public, because I want it to be
impossible to create more than one instance.

Does anybody know what to do about this?

Thanks.

Jul 17 '05 #1
10 3005
Uzytkownik <jb*****@gmail. com> napisal w wiadomosci
news:11******** **************@ g47g2000cwa.goo glegroups.com.. .
Fatal error: Access level to Member::__const ruct() must be public (as
in class User) in class.member.ph p on line 6

But I don't want the constructor to be public, because I want it to be
impossible to create more than one instance.

Does anybody know what to do about this?


Make it protected :-)

Jul 17 '05 #2
jb*****@gmail.c om wrote:
But this gives me the following error:
Fatal error: Access level to Member::__const ruct() must be public (as
in class User) in class.member.ph p on line 6

But I don't want the constructor to be public, because I want it to be
impossible to create more than one instance.

Does anybody know what to do about this?


This is just how things are. If you want to extend a class and declare the
constructor of the extending class as private, the constructor of the parent
class should also be private. The only thing you could do is to define the
constructor method in your User class as private also and add a
getInstance() method which returns a freshly made instance each time it's
called...
JW

Jul 17 '05 #3
Thanks for the replies!

@Bzdziul: I can't make it protected, because that is still a stricter
access level than public and the contructor for the User-class really
needs to be public (unless Janwillem's can work).

@Janwillem: I kind of figured that this is just the way it is... Did I
understand that your suggestion is something like this:
<?php
class User
{
private function __construct ($iUserId) {
$this->iUserId = $iUserId;
}

public static function getInstance ($iUserId) {
return new User($iUserId);
}
}
/* And instead of this:
$user = new User(23);
I would use this:
$user = User::getInstan ce(23);*/
?>

Well, I guess that could work, although I don't really like it as a
solution, because User is supposed to be a normal class like every
other. But I guess it is either this or just using a public constructor
for the Member-class.
I was wondering if it was possible to somehow throw an error if the
constructor is called from somewhere other than the class itself. In
pseudocode:
public function __construct () {
if (called from global scope)
trigger_error (__CLASS__.' is not supposed to be instantiated
from the global scope because it is a Singleton class.', E_ERROR);
}

However, I don't know what to fill in in the if-statement. Any
suggestions?

Jul 17 '05 #4
What do you think of this solution ? It's not thread-safe however...

<?php

class User { }

class Member extends User
{
private static $instance = NULL;
private static $canInstanciate = false;
public static function getInstance ()
{
if (self::$instanc e === NULL)
{
self::$canInsta nciate = true;
self::$instance = new Member();
self::$canInsta nciate = false;
}
return self::$instance ;
}

public function __construct ()
{
if (self::$canInst anciate !== true)
{
trigger_error (__CLASS__.' is not supposed to be
instantiated from the global scope because it is a Singleton class.',
E_USER_ERROR);
}
}
public function test () { echo 'Hello'; }
}

$x = Member::getInst ance();
$x->test();

$x = new Member();
$x->test();

?>

Cheers,
.... zimba

Jul 17 '05 #5
That's actually what I came up with myself yesterday night. I like it,
although I was hoping for an even more elegant way, but I think I'm
going to use this.

What does 'thread-safe' mean?

Jul 17 '05 #6
Jordi wrote:
What does 'thread-safe' mean?


When an application is thread-safe, it implies that each user gets a unique
object instance.

Jordi's example is not considered thread-safe, because it returns the same
object for all users.

The following would make the class thread-safe for multiple users:

<?php
class User {}

class Member extends User {
private static $instances = array();
private static $canInstanciate = false;
private $user_id;
private $name;

public static function getInstance($us er_id) {
if (!isset(self::$ instances[$user_id])) {
self::$canInsta nciate = true;
self::$instance s[$user_id] = new Member($user_id );
self::$canInsta nciate = false;
}
return self::$instance s[$user_id];
}

public function __construct($us er_id) {
if (self::$canInst anciate !== true) {
trigger_error(' Unable to instantiate', E_USER_ERROR);
}
$this->user_id = $user_id;
}

public function setName($name) {
$this->name = $name;
}

public function getName() {
return $this->name;
}
}
$m = Member::getInst ance(10);
$m->setName('Joe') ;
echo $m->getName(), '<br />';

$m2 = Member::getInst ance(20);
$m2->setName('Bill' );
echo $m2->getName(), '<br />';

$m3 = Member::getInst ance(10);
echo $m3->getName(), '<br />';

?>
JW

Jul 17 '05 #7
So with the script I had so far, only one member can be logged in at a
time?

I don't need to be able to do this in a script:
$m = Member::getInst ance(10);
$m->setName('Joe') ;
echo $m->getName(), '<br />';

$m2 = Member::getInst ance(20);
$m2->setName('Bill' );
echo $m2->getName(), '<br />';

$m3 = Member::getInst ance(10);
echo $m3->getName(), '<br />';

There should only be one member per script execution, but it should be
possible for two different visitors to both instantiate their own
Member-object.
In order to accomplish that, do I need your example or will mine do?

Jul 17 '05 #8
Janwillem Borleffs wrote:
Jordi wrote:
What does 'thread-safe' mean?
When an application is thread-safe, it implies that each user gets a

unique object instance.

Jordi's example is not considered thread-safe, because it returns the same object for all users.


No it won't. The script starts from scratch on each request, with no
knowledge of what happened on previous (or simultaneous) runs.

Variables (including class instances and static members) are not shared
between PHP runs.
--
Oli

Jul 17 '05 #9
Jordi wrote:
There should only be one member per script execution, but it should be
possible for two different visitors to both instantiate their own
Member-object.
In order to accomplish that, do I need your example or will mine do?


In that case, your script will do fine.
JW

Jul 17 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
3217
by: lawrence | last post by:
To call I would do something like: $headline = McSelectJustOneField::callDatastore("cbHeadline"); Is this the correct use of the static keyword, to implement a Singleton design?
21
2464
by: Sharon | last post by:
I wish to build a framework for our developers that will include a singleton pattern. But it can not be a base class because it has a private constructor and therefore can be inherit. I thought maybe a Template can be use for that, but C# does not support Templates (will be C# generics in mid 2005). Does anyone have a solution on how the singleton pattern can be written, in C#, as a framework/ infrastructure class, so users can use this...
7
2564
by: Robert W. | last post by:
I've just been reading all about the Singleton class and understand how to implement and use it but I cannot understand why one NEEDS to use it instead of just declaring a class and implementing Public Static variables therein. My usage revolves around global access to Reference Data. Some examples: - Countries, States-Provinces, Cities, Area Codes - Mobile Devices: Name, Description, Picture Filename and other sorts of data that will...
2
1799
by: Chris Murphy via DotNetMonster.com | last post by:
Hey guys, I've been hitting a brick wall with a problem I've come accross in developing an application. Background: The application uses one primary class that I'm trying to implement with the singleton pattern in mind. The design pattern seems to be working properly (complex and simple variations of it) Serialzation/Deserialzation seems to work okay. I run into a problem when I attempt to load the file back into the
4
2002
by: Michael Maes | last post by:
Hi, How would I make a class implementing a Singleton Pattern serializable. My problem is the Singleton Pattern 'requires' the constructor to be Private and serialization requires the constructor to be Public. Thanks, Michael
5
1991
by: Rich | last post by:
The following code produced a singleton object with application scope when it should have had page scope: public class Singleton { private static Singleton uniqueInstance = null; private Singleton() { } public static Singleton getInstance()
12
8954
by: Preets | last post by:
Can anyone explain to me the exact use of private constructors in c++ ?
7
4232
by: John A Grandy | last post by:
For a singleton class utilizes by ASP.NET 2.0 page processing: When initial instantiation is performed during the initial call to the retrieve instance method (let's call the method "getInstance()"), an instantiated object of the class is assigned to the class' internal static reference to that an object of itself (let's call the reference "uniqueInstance") Does that uniqueInstance have global scope in the context of page processing ?
9
3334
by: Tony Proctor | last post by:
I need to create a process Singleton object for an ASP application, but I'm having some odd issues In my GLOBAL.ASA, I have an <OBJECTelement specifying the relevant ProgID with RUNAT=Server and SCOPE=Application However, although this works fine for some classes, I get a permission denied error for classes from a particular DLL. The error response is:- Active Server Pages error '8002802b'
0
8884
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8749
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9400
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9168
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9103
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8084
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6700
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6009
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4779
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.