You are here: Re: PHP5 Singlton vs. Abstract Classes « All PHP « IT news, forums, messages
Re: PHP5 Singlton vs. Abstract Classes

Posted by Dikkie Dik on 10/13/03 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.

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
>
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация