简体   繁体   English

为什么 PHP 被认为是面向对象的?

[英]Why is PHP considered Object Oriented?

I have been reading around the definition of OOP and couldn't get why PHP is considered object oriented.我一直在阅读 OOP 的定义,但不明白为什么 PHP 被认为是面向对象的。

Can this have anything to do that the "basic level" of PHP isn't and more advanced features are?这与 PHP 的“基本级别”不同而更高级的功能有什么关系?

OO features were added to PHP in stages through versions 3-5, after much of the standard library had already been created and the language was already established.在大部分标准库已经创建并且语言已经建立之后,OO 特性通过版本 3-5 分阶段添加到 PHP。 Background背景

For this reason the standard library is not object-oriented and so everyday PHP scripts need not use any OO-style features at all.由于这个原因,标准库不是面向对象的,因此日常 PHP 脚本根本不需要使用任何 OO 风格的特性。 Although PHP by now has most of the standard features of an object-oriented language, many authors don't use them.尽管 PHP 现在具有面向对象语言的大部分标准特性,但许多作者并不使用它们。

Library functions added to the language later continued to use functional style for consistency, though many extension modules do use objects.尽管许多扩展模块确实使用对象,但后来添加到语言中的库函数继续使用函数式风格以保持一致性。

Almost any language that allows you to create and instantiate classes can be considered object oriented.几乎任何允许您创建和实例化类的语言都可以被认为是面向对象的。

PHP has these capabilities, but doesn't really stretch them. PHP 具有这些功能,但并没有真正扩展它们。 You can use OOP to help your code, but it isn't required.您可以使用 OOP 来帮助您编写代码,但这不是必需的。 Java and C# barely allow you to write non-OO code, as everything must be in a class. Java 和 C# 几乎不允许您编写非 OO 代码,因为所有内容都必须在一个类中。

Can this have anything to do that the "basic level" of PHP isn´t and more advanced features are?这与 PHP 的“基本级别”不是和更高级的功能有什么关系?

You could say that about just about any OO language.你可以说几乎任何面向对象的语言。 The general definition of OO code is where you create classes and instantiate them in your code, calling methods on them from other classes. OO 代码的一般定义是创建类并在代码中实例化它们,从其他类调用它们的方法。 Nothing stops you from using only static methods or one super class with a 'run' method that only calls other methods inside the class, both of which would definitely NOT be object oriented.没有什么能阻止您只使用静态方法或一个带有“run”方法的超类,该方法只调用类内的其他方法,这两种方法都绝对不是面向对象的。 As far as I know, there aren't any languages that say "You must create classes and instantiate them or you will be banished!"据我所知,没有任何语言说“你必须创建类并实例化它们,否则你将被放逐!” (I haven't looked into Smalltalk though). (不过我还没有研究过 Smalltalk)。

Beginners often learn the basics while putting all their code in just one method that gets called at the stat of the program.初学者经常学习基础知识,同时将所有代码放在一个方法中,该方法在程序的 stat 中被调用。 Once they get to more 'advanced' features like methods and classes, they are offered other options.一旦他们获得了更“高级”的功能,如方法和类,他们就会获得其他选择。

There is already a sufficient (and accepted) answer here, but I thought I'd throw another log on the fire for clarity's sake.这里已经有一个足够的(并被接受的)答案,但我想为了清楚起见,我会在火上再扔一根木头。

The "class" keyword (and the enforcement of its ubiquity, as in Java) does not Object-Oriented Programming make. “class”关键字(以及它无处不在的强制执行,就像在 Java 中一样)不是面向对象编程。 As CrazyJungleDrummer pointed out, it is perfectly feasible (and all too common) to write entirely procedural code in something like Java;正如 CrazyJungleDrummer 指出的那样,用 Java 之类的东西编写完全程序化的代码是完全可行的(而且非常普遍); the fact that the code lies between curly braces in a class called HelloWorld doesn't change that fact.代码位于名为 HelloWorld 的类中的大括号之间的事实并没有改变这一事实。 And just hiding a bunch of functions in a class and calling them static methods isn't OOP either -- it's namespacing.将一堆函数隐藏在一个类中并调用它们静态方法也不是面向对象编程——它是命名空间。

Think of a proper object as a struct (or "custom type", depending on your previous language exposure) that knows what to do.将适当的对象视为知道要做什么的结构(或“自定义类型”,取决于您以前的语言接触)。 Objects are data that you don't (or shouldn't) act upon directly;对象是您不(或不应该)直接操作的数据; you ask them to do things to themselves, and you ask them to tell you about themselves.你让他们为自己做事,你让他们告诉你关于他们自己的事情。 You create entities and pass messages.您创建实体并传递消息。 OOP is about treating your data like it's all grown up and can handle itself. OOP 是关于将您的数据视为已经长大并可以自行处理。 It's not about where the main line of code lives, but how data are treated.这不是关于主代码行的位置,而是如何处理数据。

Oh, and one more thing -- even in a language that is more obviously canted toward OOP, real OOP is not always the right approach.哦,还有一件事——即使在一种更明显倾向于 OOP 的语言中,真正的 OOP 并不总是正确的方法。 It's all about the data.这都是关于数据的。

您可以使用 PHP 编写,但大多数核心功能都不是面向对象的。

This answer is inspired by this Man and his answer .这个答案是受此启发,他的答案

Object-Oriented technology is often described in terms of encapsulation, polymorphism, and inheritance .面向对象技术通常用封装、多态和继承来描述 But these are only identity.但这些只是身份。 If object-oriented technology is to be successfully it must emphasis on the object.面向对象技术要取得成功就必须强调对象。

When we say Object-oriented or Object-orientation it can refer to several things:当我们说面向对象或面向对象时,它可以指以下几件事:

  1. Object-oriented analysis and design[OOAD]面向对象的分析与设计[OOAD]
  2. Object-oriented design[OAD]面向对象设计[OAD]
  3. Object-oriented database面向对象的数据库
  4. Object-oriented modeling面向对象建模
  5. Object-oriented operating system面向对象的操作系统
  6. Object-oriented programming[OOP]-->topic of concern面向对象编程[OOP]-->关注的话题
  7. Object-oriented software engineering面向对象的软件工程
  8. Object-oriented user interface面向对象的用户界面

What Pure Object Oriented Programming Language [OOP] is?什么是面向对象编程语言[OOP]

Alan Kays [ "Considered by some to be the father of object-oriented programming" ] [Defination] 5 link by Gordon : Alan Kays [ "Considered by some to be the father of object-oriented programming" ] [定义] Gordon 的5链接:

  1. EverythingIsAnObject.一切都是对象。

  2. Objects communicate by sending and receiving messages (in terms of objects).对象通过发送和接收消息(就对象而言)进行通信。

  3. Objects have their own memory (in terms of objects).对象有自己的内存(就对象而言)。

  4. Every object is an instance of a class (which must be an object).每个对象都是一个类的实例(它必须是一个对象)。

  5. The class holds the shared behavior for its instances (in the form of objects in a program list)类保存其实例的共享行为(以程序列表中的对象形式)

Now clearly it can be seen Java,C++ and PHP violates rule 1?Why bcoz int, float etc. (there are a total of eight primitive types).现在很明显可以看出 Java、C++ 和 PHP 违反了规则 1?为什么是 bcoz int, float etc. (there are a total of eight primitive types). so it cannot be Object oriented in strict sense but some folk's considered it as OOP.所以严格意义上它不能是面向对象的,但有些人认为它是面向对象的。

The general approach of OOP is to view a software system as a collection of interacting entities called "objects" each of which is defined by an identity , a state described in terms of member variables , and a behavior described in terms of methods that can be invoked OOP 的一般方法是将软件系统视为称为“对象”的交互实体的集合,每个对象由一个身份定义,用成员变量描述的状态,以及用方法描述的行为。被调用

What OOP is not?什么不是面向对象编程?

Object-Oriented technology is often described in terms of encapsulation, polymorphism, and inheritance .面向对象技术通常用封装、多态和继承来描述 But these are only identity.但这些只是身份。

An Object Oriented system, language, or environment should include at least Encapsulation, Polymorphism, and Inheritance.一个面向对象的系统、语言或环境至少应该包括封装、多态和继承。

  1. Polymorphism and Inheritance are certainly patterns that facilitate OO programming, but not only bound to it多态和继承当然是促进面向对象编程的模式,但不仅限于它

在此处输入图片说明

  1. The object-oriented paradigm isn't completely the domain of high-level programming languages -->may topic of debate but i came across this OOP in Assembly面向对象的范式并不完全属于高级编程语言的领域 --> 可能是争论的话题,但我在汇编中遇到了这个OOP

Uncle Bob aka Bob Martin in his lecture shows How C implements Encapsulation,Inheritance,and Polymorphism LINK Uncle Bob aka Bob Martin 在他的演讲中展示了 C 如何实现封装、继承和多态LINK

  1. OO is based on modeling real-world objects // For Marketing purpose OO 基于对真实世界对象的建模 // 出于营销目的

Difference Between OOP and Functional? OOP 和函数式的区别?

This may be not be perfect answer but i gave a try,Thnks to knowledge of valley .这可能不是完美的答案,但我试了一下,感谢山谷的知识

Note: Images are randomly found on google注:图片是在google上随机找到的

在此处输入图片说明

It's been a long time since this question but I came upon this article and wanted to shre the author's point of view.这个问题已经很久了,但我看到了这篇文章,想表达作者的观点。

PHP is not object oriented! PHP 不是面向对象的!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM