|
Posted by www.douglassdavis.com on 10/13/59 11:30
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
[Back to original message]
|