PHP5 Singlton与抽象类 [英] PHP5 Singlton vs. Abstract Classes

查看:78
本文介绍了PHP5 Singlton与抽象类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,首先我是OOP的新手,所以我的问题对你来说可能听起来很愚蠢。如果您能提供的唯一答案是获取有关OOP的书籍。然后就不要这么浪费你的时间和我的时间因为它已经订购了。我只是太好奇

关于这个等待这本书。


我想知道的是它是不是很好的PHP编程练习使用

抽象类而不是单例类。例如,一个登录类。

我已经把它作为一个抽象类,现在我想知道它是否是一个好的

想法。从技术上讲,只有一个登录对象,所以我认为这个对象是毫无意义的,我使用了一个抽象类,其中包含了所有内容

static。这是一个好主意还是一个坏主意,为什么?在什么情况下只有一个对象和没有任何对象只有静态

函数和变量之间的区别

会导致再使用一个?或者为什么在
PHP上下文中我更喜欢使用单个对象而不是我将要调用静态虚拟对象的内容。

谢谢,

Dae

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

推荐答案

Daedalus.OS写道:
Daedalus.OS wrote:
Ok我先对于OOP来说,这是一个新手,所以我的问题可能听起来很愚蠢。如果您能提供的唯一答案是获取有关OOP的书籍。然后不要放弃你的时间和我的时间,因为它已经订购了。我太好奇了,关于这个等待这本书。

我想知道是不是好的PHP编程实践使用
摘要类而不是单例类。


抽象类是没有对象可以实例化的类。

通常,这是因为有一些方法未定义

(摘要)。抽象类的含义是超类,因此从抽象类扩展的类可以实现剩下的内容

undefined。这可能听起来很模糊,所以让我举个例子。

假设您想支持多个数据库。您可以使用SqlToArray方法定义一个名为AbstractQuery的抽象类,它将

作为字符串,并将查询结果作为数组返回。 SQL _can_是

数据库独立,数组也是数据库独立的,所以

这个类可能很有用。您可以将方法SqlToArray定义为

abstract,并创建更多类:MysqlQuery,OdbcQuery,

都来自AbstractQuery,并实现了数据库相关的

的东西。您也可以通过界面完成此操作,作为一个界面

是一个纯粹的抽象类。


静态类是一个只有静态方法的类。最好的例子来自它的
是java中的Math类。静态类是无状态的:它们

没有可以区分一个实例与另一个实例的数据。你登录

类可能_will_有一个状态(例如LoginSucceeded),所以

这不是一个很好的选择。


单身人士是面向对象编程的全局变量。我不喜欢

他们。一个类恰好是一个对象的定义没有错。


现在要走了,等等。

...例如登录类。
我已经把它作为一个抽象类,现在我想知道它是否是一个好的想法。从技术上讲,只有一个登录对象,所以我认为这个对象毫无意义,而且我使用了一个抽象类,其中的一切都是静态的。这是一个好主意还是一个坏主意,为什么?在什么情况下只有一个对象和没有任何对象只有静态函数和变量之间的差异会导致另外一个使用另一个?或者为什么在PHP上下文中我更喜欢单个对象而不是我称之为静态虚拟对象的对象。

谢谢,
Dae
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



在文章< uM_7f.48280
In article <uM_7f.48280


yS6.27532@clgrps12>,

" Daedalus.OS" < ng@nxstream.net>写道:
yS6.27532@clgrps12>,
"Daedalus.OS" <ng@nxstream.net> wrote:
好的,首先我对OOP很新,所以我的问题可能听起来很愚蠢。如果您能提供的唯一答案是获取有关OOP的书籍。然后不要放弃你的时间和我的时间,因为它已经订购了。我太好奇了,关于这个等待这本书。

我想知道是不是好的PHP编程实践使用
摘要类而不是单例类。例如,一个登录类。
我已经把它作为一个抽象类,现在我想知道它是否是一个很好的想法。从技术上讲,只有一个登录对象,所以我认为这个对象毫无意义,而且我使用了一个抽象类,其中的一切都是静态的。这是一个好主意还是一个坏主意,为什么?在什么情况下只有一个对象和没有任何对象只有静态函数和变量之间的差异会导致另外一个使用另一个?或者为什么在PHP上下文中我更喜欢单个对象而不是我称之为静态虚拟对象的对象。

谢谢,
Dae
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




创建抽象类的部分原因是你可以在其他项目中使用

。因此,虽然你可能只有一个登录对象,但是你可能会在一百个客户面前(如果你很幸运的话)获得


谁都需要一次登录的项目对象。


然后有一天,当你得到一个项目,你需要五个登录

对象,你突然觉得你写了这个课。这是一个成本效益的事情:你必须权衡你花费在b / b
开销上的时间与未来的预计节省,这可以

有时候有点困难。


mt



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


这篇关于PHP5 Singlton与抽象类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆