简单解释 PHP OOP vs Procedural?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1530868/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:57:45  来源:igfitidea点击:

simple explanation PHP OOP vs Procedural?

phpoopprocedural-programmingprocedural

提问by Pennf0lio

I would like to learn PHP and want to get an Idea about OOP and Procedural. I read some other blogs and tutorials about OOP vs Procedural but I still can't understand the approach.

我想学习 PHP 并想了解有关 OOP 和程序的想法。我阅读了其他一些关于 OOP 与 Procedural 的博客和教程,但我仍然无法理解这种方法。

OOP vs Procedural

OOP 与程序化

  1. Which I should learn?
  2. Whats the difference in code? what are the effects?
  3. How can a PHP framework help in OOP aproach? (I would like to learn CodeIgniter)
  4. Does procedural need a Framework?
  1. 我应该学习哪个?
  2. 代码有什么区别?有什么影响?
  3. PHP 框架如何在 OOP 方法中提供帮助?(我想学习 CodeIgniter)
  4. 程序需要框架吗?

I really want to know the code difference of the both, my understanding of OOP is you create a class like and it can be access. (I dunno if thats correct).

我真的很想知道两者的代码区别,我对 OOP 的理解是你创建一个类,它可以被访问。(我不知道那是否正确)。

Thanks!

谢谢!

回答by dreftymac

Background:You asked for a "simple explanation" which suggests:

背景:您要求提供“简单解释”,这表明:

  1. You want a no-nonsense overview without jargon
  2. You want something that will help you learn from the beginning
  3. You have discovered that no two people ever answer the question the same way, and it's confusing. That's the reason you are here asking for a simple explanation. Yes?
  1. 你想要一个没有行话的严肃概述
  2. 你想要一些能帮助你从头开始学习的东西
  3. 你发现没有两个人会以同样的方式回答这个问题,这很令人困惑。这就是您在这里要求简单解释的原因。是的?

Short No-Jargon Answer:

简短的无行话答案:

  1. Many introductory explanations jump quickly into "OOP real world" examples. Those can tend to confuse more than help, so feel free to ignore that for now.
  2. You can think of source code simply as "chunks" of functionality, that just happen to be saved to individual files.
  3. There are different ways of organizing those "chunks"; depending on things like conventions of the programming language, the background and training of the developer(s), or just plain old personal preference.
  4. OOP and Procedural programming are simply two main, generally-recognized methodologies, for how to organize and arrange those "chunks" of code.
  1. 许多介绍性解释很快就跳到了“OOP 真实世界”的例子中。这些往往比帮助更容易混淆,所以现在可以随意忽略它。
  2. 您可以将源代码简单地视为功能的“块”,它们恰好被保存到单个文件中。
  3. 有不同的方式来组织这些“块”;取决于编程语言的约定、开发人员的背景和培训,或者只是简单的个人喜好。
  4. OOP 和过程式编程只是两种主要的、公认的方法论,用于组织和安排那些“代码块”。

Long No-Jargon Answer:

长的没有行话的答案:

Procedural vs OOP is just one aspect of a fundamental issue of computer programming: how to make your code easy to understandand a piece of cake to professionally maintain. You can actually write "Procedural" code that follows some of the principles of OOP, so the two are not necessarily opposites.

Procedural vs OOP 只是计算机编程的一个基本问题的一个方面:如何使您的代码易于理解和专业维护的小菜一碟。您实际上可以编写遵循一些 OOP 原则的“过程”代码,因此两者不一定是对立的。

Your understanding will really grow once you learn other object-oriented programming languages, among which, PHP is a "new kid on the block".

一旦你学习了其他面向对象的编程语言,你的理解就会真正增长,其中,PHP 是一个“新来的孩子”。

Here is a quick overviewof what you will learn as you build experience:

以下是您在积累经验时将学到的内容的快速概述

  • You can write PHP source code that does useful tasks
  • You can organize useful tasks into "chunks"of code
  • You can think of "chunks" of code independently of the individual files where they are saved
  • Sometimes those "chunks" of code will behave differently based on parametersyou pass in
  • Chunks of code that accept parameters are called "Functions"
  • Functions can be "chunked"together, and there are different ways of doing this:
    • For example:you could have just one big PHP file with all the functions you have ever written in your entire life, listed in alphabetical order by function name
    • For example:you could have multiple PHP files with functions that are chunked together by subject matter [e.g., functions for doing basic string manipulation, functions for processing arrays, functions for file input/output, etc]
  • OOP is a special way of "chunking" Functions together into a "Class"
  • A Class is just another level of "chunking"code together so that you can treat it as a unified whole
  • A Class can be thought of as a "chunking" of methodsand properties

    • methodsare simply functions that are logically related to one another in some meaningful way. The words "method" and "function" are basically two different terms for the same thing.
    • propertiesare simply data values that are related to the class. These are values that are intentionally non-isolated to any individual function, because more than oneof the functions in the class should have access to them.
      • For example: if your class has a bunch of methods for doing astronomy, properties of the class might be the values for certain famous numbers that all astronomy methods need to know about (like Pi, the speed of light, the distance between specific planets, etc.).
    • This is where most OOP explanations get confusingbecause they branch off into "real world examples" which can quickly get off-topic. Often, "real world" is a euphemism for the ontological perspectives of a particular individual. That tends to be useful only once you already understand the conceptwell enough to teach it to someone else.
    • To understand OOP without confusion, you can skip the "real world" examples for now, and just focus on the code. A Class is simply a way to store functions(aka methods) and properties(aka data) as PHP code in one or more related "chunks"where each individual "chunk" deals with a specific topic or piece of functionality. That's all you need to know in order to get started.
  • A Class is useful because it allows you to organize your code at a very high levelin a way that makes it easy for you to understand, use, and maintain.

  • When someone has written a lot of functions, and organized them into a lot of Classes, and gotten those to work together in some cool way, they package the whole thing together and call it a "Framework".
  • A Framework is just the next-highest level of "chunking"(including coding style and conventions) that one or more people agree on because they like the way the code is organized and it suits their working style, preferences, values, plans for world domination, etc.
  • 您可以编写执行有用任务的PHP 源代码
  • 您可以将有用的任务组织成代码块”
  • 您可以独立于保存它们的单个文件来考虑代码的“块”
  • 有时,根据您传入的参数,这些“代码块”的行为会有所不同
  • 接受参数的代码块称为“函数”
  • 函数可以“组合在一起,有不同的方法可以做到这一点:
    • 例如:您可以只有一个包含您一生中编写过的所有函数的大型 PHP 文件,并按函数名称的字母顺序列出
    • 例如:您可以有多个 PHP 文件,其中包含按主题组合在一起的函数 [例如,用于执行基本字符串操作的函数、用于处理数组的函数、用于文件输入/输出的函数等]
  • OOP 是一种将函数“分块”到“类”中的特殊方式
  • 只是将代码“分块”在一起的另一个级别,以便您可以将其视为一个统一的整体
  • 类可以被认为是方法属性的“组块”

    • 方法只是在逻辑上以某种有意义的方式相互关联的函数。“方法”和“功能”这两个词基本上是同一事物的两个不同术语。
    • 属性只是与类相关的数据值。这些值有意不与任何单个函数隔离,因为类中的多个函数应该可以访问它们。
      • 例如:如果您的类有一堆用于天文学的方法,则该类的属性可能是所有天文学方法都需要了解的某些著名数字的值(例如 Pi、光速、特定行星之间的距离、等等。)。
    • 这就是大多数 OOP 解释令人困惑的地方,因为它们分支到真实世界示例”中,这些示例很快就会偏离主题。通常,“现实世界”是对特定个体的本体论观点的委婉说法。只有当您已经很好地理解了该概念并将其教给其他人时,这往往才有
    • 为了不混淆地理解 OOP,您现在可以跳过“真实世界”示例,而只关注代码。类只是一种将函数(也称为方法)和属性(也称为数据)作为 PHP代码存储在一个或多个相关“块”中的方法,其中每个单独的“块”处理特定主题或功能。这就是您开始所需的全部知识。
  • 类很有用,因为它允许您以一种易于理解、使用和维护的方式在非常高的级别组织代码。

  • 当有人写了很多函数,把它们组织成很多类,并让它们以某种很酷的方式协同工作时,他们将整个东西打包在一起,称之为“框架”
  • 框架只是一个或多个人同意的下一个最高级别的“分块”(包括编码风格和约定),因为他们喜欢代码的组织方式并且适合他们的工作风格、偏好、价值观、世界计划统治等

See also

也可以看看

回答by Wim ten Brink

OOP is nothing more than a design pattern. If you're just beginning then learn the basics by focusing on the procedural approach. Most importantly, get familiar with basic principles like loops, conditions and calling other procedures.

OOP 只不过是一种设计模式。如果你刚刚开始,那么通过专注于程序方法来学习基础知识。最重要的是,熟悉基本原理,如循环、条件和调用其他过程。

While you're creating your procedural code, make a habit by adding related methods inside a single source file. Learn to divide your procedures into logical units and then you're already starting to become object-oriented. Basically, an object is nothing more than a collection of methods that are related to one another simply because they operate on the same set of data. (Not speaking of databases here, but application data!)

在创建过程代码时,养成在单个源文件中添加相关方法的习惯。学习将您的过程划分为逻辑单元,然后您就已经开始变得面向对象了。基本上,一个对象只不过是一组彼此相关的方法,仅仅是因为它们对同一组数据进行操作。(这里不是说数据库,而是应用程序数据!)

OO is mainly used to make your code more logical by dividing everything in simple blocks. By combining the right blocks, you get a complete application. OO isn't a silver bullet or golden hammer which will solve all your problems. But what it does do, is making your code easier to understand.

OO 主要用于通过将所有内容划分为简单的块来使您的代码更具逻辑性。通过组合正确的块,您将获得一个完整的应用程序。OO 不是可以解决所有问题的银弹或金锤。但它所做的,是让你的代码更容易理解。

Then again, some people still manage to make a complete mess out of objects, simply by turning them into huge super-objects with hundreds of methods. Such objects don't differ much from a regular procedural approach, simply because of the huge amount of methods being combined together without any real logic. It's a mistake that's easy to make when people start doing OOP too fast.

再说一次,有些人仍然设法将对象弄得一团糟,只需使用数百种方法将它们变成巨大的超级对象。这样的对象与常规的程序方法没有太大区别,仅仅是因为大量的方法组合在一起而没有任何真正的逻辑。当人们开始过快地执行 OOP 时,这是一个很容易犯的错误。

回答by Rawdreeg

To add on the great answers above. You should see OOP as a natural progression of your coding style -when you start writing small program you might just need to put together a couple of lines of php code, then group them into functions and the more functions you write you may feel the need to better organize them into classes. OOP just let your structure your codes better -allowing a better code maintenance.

添加上面的好答案。您应该将 OOP 视为您编码风格的自然发展——当您开始编写小程序时,您可能只需要将几行 php 代码放在一起,然后将它们组合成函数,并且您编写的函数越多,您可能会觉得需要以便更好地将它们组织成类。OOP 只是让您更好地构建代码 - 允许更好的代码维护。

回答by Edward Manda

Procedural php and oop uses the same php code. Then only difference is that with procedural, you focus on one task and that's it. In oop, you organize your code using patterns or chunks that can be re-used in many different areas of the code.

程序化 php 和 oop 使用相同的 php 代码。那么唯一的区别是程序化,你专注于一项任务,仅此而已。在 oop 中,您可以使用可以在代码的许多不同区域中重复使用的模式或块来组织代码。

Simple answer is that, you need to know and understand php. You can learn it at php.net. Once you understand it, then you can start organizing your code in into chucks.

简单的答案是,您需要了解并了解 php。您可以在 php.net 上学习。一旦你理解了它,那么你就可以开始将你的代码组织成夹子。

Procedural code uses functions, variables.

程序代码使用函数、变量。

Once you get a hang of things, you can start organizing the functions and variables into classes. We start calling the functions as methods and variables as properties.

一旦掌握了窍门,就可以开始将函数和变量组织到类中。我们开始将函数称为方法,将变量称为属性。

Good luck.

祝你好运。

回答by Joonas Pulakka

You should learn both. Objects are just one of the many possible abstractions in existence, and abstraction is what programming is ultimately all about. That said, start with procedural stuff, and then add objects later, because PHP objects' internals are procedural anyway.

两个都应该学。对象只是存在的众多可能的抽象之一,而抽象是编程的最终目的。也就是说,从程序性的东西开始,然后再添加对象,因为 PHP 对象的内部无论如何都是程序性的。

As for frameworks; first learn the fundamentals of the language, write throwaway experimental programs and such. Later you can familiarize yourself with frameworks and consider yourselfwhether youfind some of them useful in some context. They definitely aren't mandatory.

至于框架;首先学习语言的基础知识,编写一次性的实验程序等等。稍后,您可以与框架熟悉,并考虑自己是否发现在某些情况下他们中的一些有用的。它们绝对不是强制性的。