473,573 Members | 5,087 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP5 Singlton vs. Abstract Classes

Ok first I'm pretty new to OOP, so my question may sound stupid to some of
you. If the only answer you can provide is "get a book about OOP" then don't
loose your time and mine cause it's already ordered. I'm just too curious
about this one to wait for the book.

I would like to know is if it's good php programming practice to use
abstract classes instead of singleton classes. For exemple a login class.
I've made one as an abstract class and now I'm wondering if it's a good
idea. Technically there would be only one login object so I thought having
this object was pointless and I use an abstract class with everything in it
static. Is it a good or a bad idea and why? In what situation the difference
between having only one object and no object at all with only static
functions and variables would lead to use one more the other? Or why in a
PHP context would I prefer to have a single object rather than what I would
call a "static virtual object".
Thanks,
Dae
Oct 27 '05 #1
12 3077
Daedalus.OS wrote:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of
you. If the only answer you can provide is "get a book about OOP" then don't
loose your time and mine cause it's already ordered. I'm just too curious
about this one to wait for the book.

I would like to know is if it's good php programming practice to use
abstract classes instead of singleton classes.
Abstract classes are classes of which no object can be instantiated.
Normally, this is because there is some method left undefined
(abstract). The meaning of an abstract class is to be a superclass, so a
class extending from the abstract class can implement what was left
undefined. This probably sounds very vague, so let me give you an example.
Suppose you wantr to support more than one database. You could define an
abstract class called AbstractQuery with a SqlToArray method, that takes
a string and returns the result of the query as an array. SQL _can_ be
database independent and arrays are database independent as well, so
this class could be useful. You could define the method SqlToArray as
abstract, and create a few more classes: MysqlQuery, OdbcQuery,
FirefoxQuery, or whatever database you want to support. These classes
all extends from the AbstractQuery and implement the database-dependent
stuff. You could have done this also with an interface, as an interface
is a purely abstract class.

A static class is a class with only static methods. The best example
from it is the Math class in java. Static classes are stateless: they
have no data that can distinguish one instance from the other. You login
class probably _will_ have a state (LoginSucceeded , for instance), so
this is not a very good option.

Singletons are the global variables of OO programming. I don't like
them. There is nothing wrong with a class that happens to be the
definition of just one object.

Gotta go now, more later.
... For exemple a login class.
I've made one as an abstract class and now I'm wondering if it's a good
idea. Technically there would be only one login object so I thought having
this object was pointless and I use an abstract class with everything in it
static. Is it a good or a bad idea and why? In what situation the difference
between having only one object and no object at all with only static
functions and variables would lead to use one more the other? Or why in a
PHP context would I prefer to have a single object rather than what I would
call a "static virtual object".
Thanks,
Dae

Oct 27 '05 #2
In article <uM_7f.48280$yS 6.27532@clgrps1 2>,
"Daedalus.O S" <ng@nxstream.ne t> wrote:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of
you. If the only answer you can provide is "get a book about OOP" then don't
loose your time and mine cause it's already ordered. I'm just too curious
about this one to wait for the book.

I would like to know is if it's good php programming practice to use
abstract classes instead of singleton classes. For exemple a login class.
I've made one as an abstract class and now I'm wondering if it's a good
idea. Technically there would be only one login object so I thought having
this object was pointless and I use an abstract class with everything in it
static. Is it a good or a bad idea and why? In what situation the difference
between having only one object and no object at all with only static
functions and variables would lead to use one more the other? Or why in a
PHP context would I prefer to have a single object rather than what I would
call a "static virtual object".
Thanks,
Dae


Part of the point of creating an abstract class is so that you can use
it in other projects. So while you may only have one login object, you
might wind up down the road (if you are lucky) with a hundred clients
who all need projects with one login object.

And then comes a day when you get a project where you need five login
objects, and you suddenly become glad you wrote the class.

It's a cost-benefit thing: you have to weigh the time you spend on
overhead against the projected savings in the future, which can
sometimes be a bit difficult.

mt
Oct 27 '05 #3
More about singletons:

A singleton is a static class that has an accessor for exactly one
instance of the class. This is then the only instance that can be
created. There are a few drawbacks to this:
- If you ever need more than one object of that kind, you have a
problem. And if you write unit tests, you do have to create another
instance just for the test. Singletons are a real pain to test and don't
scale at all.
- The instance is not passed via the interface (= the publicly available
methods) of the class. Effectively, a singleton is a global read-only
variable that is set automatically. If you want to replace it (for
testing, for instance), you again have a problem.
The fact that the singleton is not passed through the interface
"hardwires" it to any place in the code where it is used. This obscures
the class dependencies in the code.

So where would I put the instance if I don't want a singleton? On a
small php page I could just use a plain non-object oriented variable. On
more complex applications, I have an object structure with some root
object that contains the data that is accessible within that section.
This root object is responsible for this data (either by checking the
input parameters of its constructor or by creating the necessary
instances itself).

Best regards

Daedalus.OS wrote:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of
you. If the only answer you can provide is "get a book about OOP" then don't
loose your time and mine cause it's already ordered. I'm just too curious
about this one to wait for the book.

I would like to know is if it's good php programming practice to use
abstract classes instead of singleton classes. For exemple a login class.
I've made one as an abstract class and now I'm wondering if it's a good
idea. Technically there would be only one login object so I thought having
this object was pointless and I use an abstract class with everything in it
static. Is it a good or a bad idea and why? In what situation the difference
between having only one object and no object at all with only static
functions and variables would lead to use one more the other? Or why in a
PHP context would I prefer to have a single object rather than what I would
call a "static virtual object".
Thanks,
Dae

Oct 27 '05 #4
ok...two thumbs down so far for singletons...on ly one citing specifics.
singletons are *great* in my opinion. you just know how, when, and where to
use them.

the example of using a global variable/function/class to achieve a
work-in-place-of a singleton is a false notion. while you may achieve the
same or desired effect(s), you cannot enforce the same scope as a singleton
without a lot of programming (and praying that another developer on your
team doesn't much things up...and understands what you're doing...to which
end he'd simply say "shit, why didn't this idiot just create a singleton and
be done with it!").

and the notion of being pigeon-holed into not scaling is, likewise, a farse.
singletons do what they are designed to do. if you had other intentions from
the beginning, then design you system differently...e lse if the requirements
change, exactly how much work does it take to convert a singleton into a
multi-instancing class?

you may want to wait for your book to arrive before assuming either of the
above reasons make singletons "bad".
Oct 27 '05 #5
oh, and one example...singl etons that simply hold constants...lea ving a
clean name-spaced definition of what they are associated with.
"steve" <a@bc.com> wrote in message news:LI******** **********@fe05 .lga...
| ok...two thumbs down so far for singletons...on ly one citing specifics.
| singletons are *great* in my opinion. you just know how, when, and where
to
| use them.
|
| the example of using a global variable/function/class to achieve a
| work-in-place-of a singleton is a false notion. while you may achieve the
| same or desired effect(s), you cannot enforce the same scope as a
singleton
| without a lot of programming (and praying that another developer on your
| team doesn't much things up...and understands what you're doing...to which
| end he'd simply say "shit, why didn't this idiot just create a singleton
and
| be done with it!").
|
| and the notion of being pigeon-holed into not scaling is, likewise, a
farse.
| singletons do what they are designed to do. if you had other intentions
from
| the beginning, then design you system differently...e lse if the
requirements
| change, exactly how much work does it take to convert a singleton into a
| multi-instancing class?
|
| you may want to wait for your book to arrive before assuming either of the
| above reasons make singletons "bad".
|
|
Oct 27 '05 #6
And even with, for example, the Math class (static class), would the right
way to go would be to create a normal class with static funtions and a
private constructor (since there can be no instance of that class) rather
than using an abstract class with static functions ?

If I get things right, abstract classes are pretty much like interfaces (at
least in PHP). The difference being that interfaces only declare functions
that need to be defined in the "child" classes and abstract classes can
declare functions (abstract functions) and/or define functions. It probably
not represents the whole picture but am I heading in the right direction ?

Thanks Dae
Abstract classes are classes of which no object can be instantiated.
Normally, this is because there is some method left undefined (abstract).
The meaning of an abstract class is to be a superclass, so a class
extending from the abstract class can implement what was left undefined.
This probably sounds very vague, so let me give you an example.
Suppose you wantr to support more than one database. You could define an
abstract class called AbstractQuery with a SqlToArray method, that takes a
string and returns the result of the query as an array. SQL _can_ be
database independent and arrays are database independent as well, so this
class could be useful. You could define the method SqlToArray as abstract,
and create a few more classes: MysqlQuery, OdbcQuery, FirefoxQuery, or
whatever database you want to support. These classes all extends from the
AbstractQuery and implement the database-dependent stuff. You could have
done this also with an interface, as an interface is a purely abstract
class.

A static class is a class with only static methods. The best example from
it is the Math class in java. Static classes are stateless: they have no
data that can distinguish one instance from the other. You login class
probably _will_ have a state (LoginSucceeded , for instance), so this is
not a very good option.

Singletons are the global variables of OO programming. I don't like them.
There is nothing wrong with a class that happens to be the definition of
just one object.

Gotta go now, more later.
... For exemple a login class. I've made one as an abstract class and now
I'm wondering if it's a good idea. Technically there would be only one
login object so I thought having this object was pointless and I use an
abstract class with everything in it static. Is it a good or a bad idea
and why? In what situation the difference between having only one object
and no object at all with only static functions and variables would lead
to use one more the other? Or why in a PHP context would I prefer to have
a single object rather than what I would call a "static virtual object".
Thanks,
Dae

Oct 27 '05 #7
Daedalus.OS wrote:
And even with, for example, the Math class (static class), would the right
way to go would be to create a normal class with static funtions and a
private constructor (since there can be no instance of that class) rather
than using an abstract class with static functions ?
The PHP help does not say anything about private constructors. I guess
it is a java thing to make them private to disallow instantiation. If
you think about it, it is quite odd. The constructor is never called
directly, but it is a language construction. You give a "new" keyword,
some space in memory is reserved, the class name is attached to it as a
type, and its constructor is called. By whom? By the virtual machine
itself, basically. Making it private is _really_ odd. As the object is
still unreferenced, _no_ object can call it (the reference is only
returned when the constructor has finished). Even the object itself
cannot call it, as it is still in the process of coming to existence.
It is not very consequent either. "private" in java means "visible only
within the same instance". Only for constructors it suddenly means
"visible within the same class".
Declaring it abstract will certainly stop instantiation. However,
"abstract", communicates that it is meant as a superclass. I would not
create a constructor at all and not declare it abstract either. As the
static operator differs from the normal method calling, it is clear
enough that instantiation is pointless. Any instance does not have
properties or methods that can be called on it.

If I get things right, abstract classes are pretty much like interfaces (at
least in PHP). The difference being that interfaces only declare functions
that need to be defined in the "child" classes and abstract classes can
declare functions (abstract functions) and/or define functions. It probably
not represents the whole picture but am I heading in the right direction ?


Yes you are. An abstract class can have some implementation, for
instance as a default implementation for its subclasses. Often, abstract
classes are more general and its subclasses more specific. If the
general stuff is defined in the abstract superclass, the subclasses only
have to fill in the details.
The "problem" with abstract classes is that you can extend only one
abstract class, whereas you can implement more than one interface.
Oct 27 '05 #8
Dikkie Dik said the following on 27/10/2005 23:20:
Daedalus.OS wrote:
And even with, for example, the Math class (static class), would the
right way to go would be to create a normal class with static
funtions and a private constructor (since there can be no instance of
that class) rather than using an abstract class with static functions ?

The PHP help does not say anything about private constructors. I guess
it is a java thing to make them private to disallow instantiation.


Not just a Java thing.

If you think about it, it is quite odd. The constructor is never called
directly, but it is a language construction. You give a "new" keyword,
some space in memory is reserved, the class name is attached to it as a
type, and its constructor is called. By whom? By the virtual machine
itself, basically. Making it private is _really_ odd.
Actually, it's a *very* common design pattern in many OO languages.

It is not very consequent either. "private" in java means "visible only
within the same instance". Only for constructors it suddenly means
"visible within the same class".
Not true. "Private" means visible within the same class, e.g.:

class Foo
{
private $v;

function bar($theFoo)
{
echo $theFoo->$v;
}
}

$a = new Foo;
$b = new Foo;
$b->bar($a);

The equivalent works in Java, C++ and C#.

Declaring it abstract will certainly stop instantiation. However,
"abstract", communicates that it is meant as a superclass. I would not
create a constructor at all and not declare it abstract either. As the
static operator differs from the normal method calling, it is clear
enough that instantiation is pointless. Any instance does not have
properties or methods that can be called on it.


One of the ideas of writing good OO code is to explicitly enforce a
well-defined interface on your classes' users, i.e. what they can and
can't do with it. If you don't want them to be able to instantiate a
Foo, enforce it with a private constructor, don't leave it to chance.

Another example would be, if it makes no semantic sense for a Foo to
ever be extended, explicitly say so with the "final" keyword.

--
Oli
Oct 28 '05 #9

Daedalus.OS wrote:
Ok first I'm pretty new to OOP, so my question may sound stupid to some of
you. If the only answer you can provide is "get a book about OOP" then don't
loose your time and mine cause it's already ordered. I'm just too curious
about this one to wait for the book.

I would like to know is if it's good php programming practice to use
abstract classes instead of singleton classes. For exemple a login class.
I've made one as an abstract class and now I'm wondering if it's a good
idea. Technically there would be only one login object so I thought having
this object was pointless and I use an abstract class with everything in it
static. Is it a good or a bad idea and why? In what situation the difference
between having only one object and no object at all with only static
functions and variables would lead to use one more the other? Or why in a
PHP context would I prefer to have a single object rather than what I would
call a "static virtual object".


some very general hints about OO:

abstract classes and singleton classes are for totally different
reasons.

if you want to create a class where you can't create a new object
(instantiate), make it final and make the constructor private. usually
this is with classes that have all static members. If you just make it
abstract, some one could extend it, then still instantiate one.

if you want to create a partial class, that can be extended to do many
different things, make an abstract class.

if you want to create something similar to functions, with no objects
attached use static methods.

if you want to create a variable similar to a non OO variable , or one
that is global in scope, use a static variable.

if you are sure you only need something like old-style functions:

login(name, password)
logout();

go with all static methods, make impossible to instantiate (as i
mentioned above). nothing really wrong with that.

if you want to have more OO functionality (inheritance, passing object
into a function, methods having some shared data (instance vars), go
with the singleton. Or if you think you will have more OO
functionality in the future, go with the singleton, because it will be
hard to recode once you make everything all static.

don't create a class with a bunch of static vars that are changed by
and used by static methods. it is misleading. people will think of
those methods more just like regular non OO functions (like cos() or
sqr() ). when actually, you are treating them as an object

Oct 28 '05 #10

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

Similar topics

1
2598
by: Matthew Bates | last post by:
Hi, I would like to use PHP5 to interact with an XML-RPC web service. I've noticed a number of libraries on the net however many are out of date and not specific to PHP5. Could anybody please recommend the best way to use XML-RPC and PHP5? Thanks in advance.
7
2160
by: Rob Long | last post by:
Hey I've noticed a somewhat annoying feature in PHP5 when it comes to extended classes and types. If I declare an interface and a class that implements it, I get a "compile" error (read parse error) if I don't actually implement the interface... expected behaviour. E.g.
0
1669
by: Yarco | last post by:
I have just installed phpdocumentor on using: pear.bat install phpdocumentor-beta to suitable my php version. Then i create a test file "test.php" to have a trial. But the result is: E:\yarco\workshop\web\php>phpdoc.bat -f test.php -pp on -s on -o HTML:frames:default -t test
2
9594
by: Dave Veeneman | last post by:
Is is legal to declare abstract members in non-abstract classes? How about non-abstract members in abstract classes? I am writing a base class with three derived classes. The base class will define the behavior for most, but not all of its members. The derived classes will define the behavior for the remaining members (the undefined...
9
5187
by: Sean Kirkpatrick | last post by:
To my eye, there doesn't seem to be a whole lot of difference between the two of them from a functional point of view. Can someone give me a good explanation of why one vs the other? Sean
4
2988
by: DBC User | last post by:
I have a class with bunch of static methods. I could regourp all the static methods into seperate 3 or 4 classes. I was thinking about using Singlton pattern for all these 4 classes so that it behaves like static still class. But my concern is, when going from static to singlton, I need to add one more line to make sure the class is...
7
4460
by: jason | last post by:
In the microsoft starter kit Time Tracker application, the data access layer code consist of three cs files. DataAccessHelper.cs DataAcess.cs SQLDataAccessLayer.cs DataAcccessHelper appears to be checking that the correct data type is used DataAcess sets an abstract class and methods
10
3483
by: DBC User | last post by:
Does any one know how to implment a singlton in c#. I have done the implementation but I ran in to "Double-checked locking alert" implemenation and not sure how to implement it. Or let me put it this way, what is the equivalent of the following java code to resolve the double-checked locking alert??? private volatile static Singlton...
0
2816
by: emin.shopper | last post by:
I had a need recently to check if my subclasses properly implemented the desired interface and wished that I could use something like an abstract base class in python. After reading up on metaclass magic, I wrote the following module. It is mainly useful as a light weight tool to help programmers catch mistakes at definition time (e.g.,...
0
7792
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...
0
8218
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7800
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...
0
8091
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...
0
6435
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...
1
5605
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...
0
5296
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...
1
2228
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1330
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.