简单解释 PHP OOP 与过程式?

发布于 2024-08-07 03:44:56 字数 1701 浏览 7 评论 0 原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

何时共饮酒 2024-08-14 03:44:56

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

  1. 您想要一个没有废话的概述没有行话
  2. 您想要一些可以帮助您从头开始学习的东西
  3. 您发现没有两个人会以相同的方式回答问题,这令人困惑。这就是您在这里要求简单解释的原因。是的?

简短的无行话答案:

  1. 许多介绍性解释都会快速跳入“OOP 现实世界”示例。这些可能会带来更多的困惑而不是帮助,所以现在可以忽略它。
  2. 您可以将源代码简单地视为功能“块”,它们恰好保存到单个文件中。
  3. 组织这些“块”的方式有多种;取决于编程语言的约定、开发人员的背景和培训,或者只是简单的个人偏好等。
  4. 面向对象编程和过程编程只是两种主要的、普遍认可的方法,用于了解如何组织和安排这些代码“块”。

长而无行话的答案:

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

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

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

  • 您可以编写执行有用任务的 PHP 源代码

  • 您可以将有用任务组织成“块”代码

  • 您可以将代码“块”独立于保存它们的各个文件

  • 有时,这些代码“块”会根据您传递的参数而表现不同

  • 接受参数的代码块称为“函数”

  • 函数可以“分块”在一起,并且有不同的方法可以做到这一点:

    • 例如:您可能只有一个大型 PHP 文件,其中包含您一生中编写过的所有函数,并按函数名称的字母顺序列出
    • 例如:您可以拥有多个 PHP 文件,其中的函数按主题组合在一起 [例如,用于执行基本字符串操作的函数、用于处理数组的函数、用于文件输入/输出的函数、等等]
  • OOP 是一种将函数“分块”在一起成为“类”的特殊方式

  • 类是只是“分块”代码的另一个级别,以便您可以处理它作为一个统一的整体

  • 类可以被视为方法的“分块” strong>和属性

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

  • 当有人编写了很多函数,并将它们组织成很多类,并让它们以某种很酷的方式一起工作时,他们将整个东西打包在一起,并将其称为“框架”.


  • 框架只是一个或多个人同意的下一个最高级别的“组块”(包括编码风格和约定),因为他们喜欢代码的组织方式并且适合他们的工作方式、偏好、价值观、统治世界的计划等。

另请参阅

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?

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.

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 understand and 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.

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

Here is a quick overview of 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 parameters you 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 methods and properties

    • methods are 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.
    • properties are simply data values that are related to the class. These are values that are intentionally non-isolated to any individual function, because more than one of 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 confusing because 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 or group. That tends to be useful only once you already understand the concept well 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 level in 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.

See also

小草泠泠 2024-08-14 03:44:56

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

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

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

话又说回来,有些人仍然设法把对象搞得一团糟,只是通过数百种方法将它们变成巨大的超级对象。此类对象与常规过程方法没有太大区别,只是因为大量方法组合在一起而没有任何真正的逻辑。当人们开始太快地进行 OOP 时,就很容易犯这个错误。

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.

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.

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.

帅气尐潴 2024-08-14 03:44:56

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

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.

家住魔仙堡 2024-08-14 03:44:56

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

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

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.

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 yourself whether you find some of them useful in some context. They definitely aren't mandatory.

递刀给你 2024-08-14 03:44:56

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

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

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

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

祝你好运。

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.

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.

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文