类有多重要? (PHP) [英] How important are classes? (PHP)

查看:95
本文介绍了类有多重要? (PHP)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不太了解类,但是有一个合理的PHP / MySQL的知识。
但为什么要学习课程?我知道他们很重要,但我可以看到使用他们的好处,我不能?

I dont know much about classes, but have a reasonable knowledge of PHP/MySQL. But why should I learn classes? I know they are important but what benefits can I see using them that I cant with?

推荐答案

实际上,在php中使用基于函数的编程通常比面向对象更简单。有很多方法,只是使用函数库,并试图保持你的代码简单和直接使你的php脚本更可维护,特别是如果你最小化状态,以提高重复性。

Actually, I've found that it is often simpler to use function-based programming in php than object oriented. There are a lot of ways in which just using function libraries and trying to keep your code simple and direct make your php scripts more maintainable, especially if you minimize state to increase reproducibility.

Objects&类是你应该知道的一个工具,所以你可以在不同的选项之间进行选择,但肯定不是唯一的选择,现在php 5.3有第一类函数,移动更多的方向真正的功能编程是另一个工具,你可以知道。

Objects & classes are one tool that you should get to know so that you can choose between the different options, but certainly not the only choice, now that php 5.3 has first class functions, moving more in the direction of true functional programming is another tool that you could get to know.

Php的背景是非常基于函数的,提供的主要语言的一大部分是函数,有时候对象的正方形不适合php的圆孔。

Php's background is very function-based, a huge portion of the native language provided is functions and sometimes the square peg of objects doesn't fit in php's round hole.

这当然不适用于Java,但php的背景是根植于函数。

This certainly wouldn't apply to Java, but php's background is very rooted in functions.

编辑:

让我们清楚,要在php上有效,你必须有一个很好的对象,因为你要去经常遇到它,如果你为php的其他人工作,你可能需要写自己的OO自己代表你的雇主。所以得知道。但是不要停留在OO,并认为它是全终端解决方案。事实上,当涉及到处理其他人的坏的PHP代码,我发现固体基于函数的编程通常是一个更简单的工具来重构和清理坏的代码。

Let's be clear, to be effective at php you will have to have a good grasp of objects, because you are going to encounter it frequently, and if you work for other people in php, you'll probably have to write OO yourself on your employer's behalf. So get to know it well. But don't stop at OO and consider it the be-all-end-all solution. In fact, when it comes to dealing with other people's bad php code, I have found solid function-based programming to often be a simpler tool for refactoring and cleaning up bad code.

我会说有几种方法来编写php代码:

I would say there are a few ways to write php code:


  1. 错误的程序代码(可重用性低,可能使用函数,

  2. 良好的基于​​函数的代码(最大化的可重用性,最小化的复杂性状态,关注点的分离)

  3. 面向对象的错误代码(大型多面对象,复杂层次结构等)

  4. 良好的面向对象代码(特定任务对象,明确区分关注点如MVC)

  1. Bad procedural code (little reusability, probably uses functions, but not well, probably uses objects, but not well).
  2. Good function-based code (maximized reusability, minimized complexities of state, separation of concerns)
  3. Bad object oriented code (large multifaceted objects, complex hierarchies, etc)
  4. Good object oriented code (specific-task objects, clear separation of concerns like MVC)

最后,随着PHP 5.3的成熟,我们将能够开始在基于函数的代码类别中引入更多的函数式编程将成为更加有效的替代品。在此期间,虽然,舒适的2和4,因为你需要两个。

And eventually, as php 5.3 matures, we'll be able to start throwing in a bit more functional programming into the "good function-based code" category and it will become an even more effective alternative. In the meantime, though, get comfortable with 2 and 4 because you'll need 'em both.

这篇关于类有多重要? (PHP)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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