使用类而不是函数的优点? [英] Advantages of using classes over functions?

查看:131
本文介绍了使用类而不是函数的优点?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




一般来说,如果我重新使用代码,我会使用一个函数。如果我需要在多个页面上使用这些

函数,我将函数写入包含

文件,其中所有页面都可以访问。


所以什么时候应该使用PHP类。我已经学会了如何将
放在一起PHP类,但从未见过使用它们的理由我在

只能使用一个函数。


我基本上只想知道课程对课程的好处。如果

可以更好地将某些东西放在一个类中,我希望以这种方式做到这一点。有什么建议吗?


干杯


Burnsy

Hi,

Generally if I re-use code, I use a function. If I need to use these
functions over a number of pages I write the function to an include
file where all pages have access.

So when should I ever use PHP classes instead. I have learned how to
put together PHP classes but never seen a reason to use them where I
can simply use a function.

Im basically just wanting to know classes benefits over functions. If
something can be better put together in a class I would like to be
doing it that way. Any suggestions?

Cheers

Burnsy

推荐答案

2005-05-09, bi******@yahoo.co.uk <双****** @ yahoo.co.uk>写道:
On 2005-05-09, bi******@yahoo.co.uk <bi******@yahoo.co.uk> wrote:
我基本上只是想知道课程对功能的好处。如果某个课程可以更好地组合在一起,我希望能够这样做。有什么建议吗?
Im basically just wanting to know classes benefits over functions. If
something can be better put together in a class I would like to be
doing it that way. Any suggestions?




你问的是为什么面向对象编程(OOP)比程序编程更好。
。答案是:这完全取决于手头的

任务。一般来说,OOP为您提供了一些管理大型系统中复杂性的工具。它还为eaiser重用提供了一种意义,即现有代码的b $ b。 OOP的一个很好的例子可以在这里找到:


< http://www.sitepoint.com/forums/showthread.php?t = 59898#post439958>


-

干杯,

- Jacob Atzen



What you''re asking is why Object Oriented Programming (OOP) is better
than procedural programming. The answer is: That wholly depends on the
task at hand. Generally speaking OOP gives you some tools for managing
complexity in larger systems. It also provides a mean for eaiser reuse
of existing code. A good example of the power of OOP can be found here:

<http://www.sitepoint.com/forums/showthread.php?t=59898#post439958>

--
Cheers,
- Jacob Atzen


< a href =mailto:bi ****** @ yahoo.co.uk> bi ****** @ yahoo.co.uk 写道:
bi******@yahoo.co.uk wrote:

一般来说,如果我重新使用代码,我会使用一个函数。如果我需要在多个页面上使用这些
函数,我将函数写入包含所有页面都可以访问的包含文件。

那么我何时应该使用PHP类代替。我已经学会了如何组合PHP课程,但从未见过使用它们的理由我可以简单地使用一个函数。

我基本上只是想知道课程的好处而已功能。如果某个课程可以更好地组合在一起,我希望能够这样做。有什么建议吗?
Hi,

Generally if I re-use code, I use a function. If I need to use these
functions over a number of pages I write the function to an include
file where all pages have access.

So when should I ever use PHP classes instead. I have learned how to
put together PHP classes but never seen a reason to use them where I
can simply use a function.

Im basically just wanting to know classes benefits over functions. If
something can be better put together in a class I would like to be
doing it that way. Any suggestions?




这是一个意见问题。一个极端说明了这样一个事实,即如果没有它们,你就不能拥有任何你不能拥有的东西,而且说为什么要打扰并且不会使用它们。另一个极端无法编码Hello,World!没有5

层的继承和十几个复合对象。


所以真正的问题是,它们对哪些工作有益?另外,你建立了什么样的

网站?你有什么麻烦吗?


在我们的系统中我们编写数据库应用程序,我们使用调度程序和

函数,以及一个存储批量的方案

准备执行中的配置参数包括填充关联数组的文件。这个

可以处理99%的情况。


我发现它实际上更容易上课的唯一地方是

提供默认行为的覆盖。我们有一个单独的类

包含一些非常基本的行为,并包含不同点的调用

来清空类方法。如果我需要覆盖默认行为,我会为该页面编制

a类(通常我不需要大多数页面的类,

库知道如何do),并编码其中一个或两个触发方法

来推动一个方向或另一个方向的处理。


但至于制造一类 - 每桌,哎呀。


-

Kenneth Downs

安全数据软件公司

(Ken)nneth @(Sec)ure(Dat)a(.com)



This is a matter of opinion. One extreme states the fact that classes give
you nothing you can''t have without them, and says why bother and does not
use them at all. The other extreme can''t code "Hello, World!" without 5
layers of inheritance and a dozen composite objects.

So the real question is, what jobs are they good for? Also, what kind of
site are you building? Is there something you are having trouble doing?

In our system we write database applications, we use a dispatcher and
functions, plus a scheme for storing lots of config parms in
ready-to-execute include files that populate associative arrays. This
takes care of 99% of cases.

The only place where I have found it actually easier to use a class is for
providing overrides to default behavior. The one single class we have
contains some very basic behaviors, and contains calls at different points
to empty class methods. If I need to override default behavior, I make up
a class for that page (normally I don''t need a class for most pages, the
libraries know what to do), and code up one or two of those trigger methods
to nudge processing in one direction or another.

But as for making one-class-per-table, yuck.

--
Kenneth Downs
Secure Data Software, Inc.
(Ken)nneth@(Sec)ure(Dat)a(.com)


Burnsy,
Burnsy,
一般如果我重新使用代码,我使用了一个函数。如果我需要在多个页面上使用这些
函数,我将函数写入包含所有页面都可以访问的包含文件。

那么我何时应该使用PHP类代替。我已经学会了如何组合PHP课程,但从未见过使用它们的理由我可以简单地使用一个函数。

我基本上只是想知道课程的好处而已功能。如果某个课程可以更好地组合在一起,我希望能够这样做。有什么建议吗?
Generally if I re-use code, I use a function. If I need to use these
functions over a number of pages I write the function to an include
file where all pages have access.

So when should I ever use PHP classes instead. I have learned how to
put together PHP classes but never seen a reason to use them where I
can simply use a function.

Im basically just wanting to know classes benefits over functions. If
something can be better put together in a class I would like to be
doing it that way. Any suggestions?



通常我会编写大量的对象和简单的函数以及

配置数组。


我通常做的是将我的所有业务逻辑放入

有行动的类中。然后,最后一张海报指出,你可以随时扩展这些操作,使其具有极高的可扩展性。


我用于对象的大部分内容都是身份验证,也是数据库。

当我转移到不同的数据库时,这会有所帮助(必须这样做几次,而不是很好,当事情只被编码到一个特定项目时)。

关于对象的另一个好处是它让你有机会将所有

的函数组合在一起并拥有可以全局的变量

脚本中的函数但不是全局函数。这可以在许多情况下大量帮助
。 (模板系统是这类

重要性的巨大例子)。


我确定稍后会发布更多内容,但如前所述

就是你的意见。


Mike


Normally I code a massive amount of objects and simple functions and
arrays of configuration.

What I normally do is put all of my business logic into classes that
have actions. Then as the last poster noted that you can always extend
those operations which makes it extremely scalable.

Most things that I use for objects are authentication and also database.
This helps when I transfer to different DBs (have had to do it a
couple times, not pretty when things are just coded to one specific item).
Another nice thing about objects is it gives you a chance to group all
of your functions and have variables that can be global to all the
functions but not global within the scripts. That can help massively on
many cases. (template systems are huge examples of this type of
importance).

I''m sure there will be more stuff posted later but as stated before it
is a all about your opinion.

Mike


这篇关于使用类而不是函数的优点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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