PHP 是面向对象的吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4699519/
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
Is PHP Object-oriented?
提问by avon_verma
Is PHP an object-oriented language? If not, then what about the framework CakePHP? Is it an object-oriented MVC implementation of PHP?
PHP 是面向对象的语言吗?如果没有,那么框架 CakePHP 呢?它是PHP的面向对象的MVC实现吗?
Also, can a PHP application wholly built using classesbe called object-oriented?
另外,完全使用类构建的 PHP 应用程序可以称为面向对象吗?
回答by tere?ko
No, PHP is not fully object oriented language.
不,PHP 不是完全面向对象的语言。
And neither is C++ or Java, because they all have primitive types(and PHP also has a huge collection of function like str_replace()
and is_*()
, which are clearly procedural in nature). Only pure object-oriented language, that i know of, are Ruby and Scala (and one could argue that latter is more aiming at functional programming paradigm).
C++ 或 Java 也不是,因为它们都有原始类型(而且 PHP 也有大量的函数,如str_replace()
and is_*()
,它们本质上是过程性的)。据我所知,只有纯面向对象语言是 Ruby 和 Scala(有人可能会争辩说后者更针对函数式编程范式)。
PHP is, what one could call, "object-capable language".
PHP 是可以称之为“对象能力语言”的语言。
As for the code written in PHP, you have to understand that just because you are using classes, it does not make it OOP. Especially if your code is mostly based on static class.
至于用PHP编写的代码,你必须明白,仅仅因为你在使用类,它并不会使它成为OOP。特别是如果您的代码主要基于静态类。
So, if you ask: "is CakePHP an OO framework?", then the answer is - NO. The most flattering description for it would be "class oriented programming". The code-base is filled with static methods and variables, where class acts more like a namespace. Basically CakePHP is a procedural code, wrapped in syntax, which on surface mimics object oriented code.
所以,如果你问:“CakePHP 是一个面向对象的框架吗?”,那么答案是 - NO。对它最讨人喜欢的描述是“面向类的编程”。代码库充满了静态方法和变量,其中类更像是一个命名空间。基本上 CakePHP 是一个程序代码,包裹在语法中,表面上模仿面向对象的代码。
回答by GolezTrol
Yes, the latest versions of PHP are object oriented. That is, you can write classes yourself, use inheritance, and where appropriate, the built in functionality is built in objects too (like MySQL features).
是的,最新版本的 PHP 是面向对象的。也就是说,您可以自己编写类,使用继承,并且在适当的情况下,内置功能也内置在对象中(如 MySQL 功能)。
There are still a lot of loose functions however, so there might be a disagreement about how object oriented PHP is. I think it is. And yes CakePHP is an object oriented framework.
然而,仍然有很多松散的函数,所以对于面向对象的 PHP 可能存在分歧。我觉得是这样的。是的,CakePHP 是一个面向对象的框架。
回答by CodePlateau
PHP is not fully object oriented but it supports some feature like
PHP 不是完全面向对象的,但它支持一些功能,例如
1) class
2) object
3) Constructors and Destructors
4) Object Inheritance
5) Scope Resolution Operator (::)
1) 类
2) 对象
3) 构造函数和析构函数
4) 对象继承
5) 范围解析运算符 (::)
and many more. If you want to learn object oriented php refer the below reference link
还有很多。如果你想学习面向对象的 php,请参考下面的参考链接
回答by WhiteWabbit
For any language to be classified as Object Oriented it needs to adhere to at least 3 principles of OO: Inheritance, Polymorphism, and Encapsulation. I don't know PHP and I have never used it before but if it at least meets these 3 principles then it is OO otherwise it is not. My suggestion is to search for how PHP supports, or not, each principle. Developers tend to be overly attached to the language they currently use and often are misguided and biased. Take for example the person stating that C++ isn't an object orient programming and yet receiving many thumbs up! This person could not be further from the truth yet his answer appears to be correct to many!
对于任何被归类为面向对象的语言,它至少需要遵守 OO 的 3 条原则:继承、多态和封装。我不知道 PHP,我以前从未使用过它,但如果它至少满足这 3 个原则,那么它就是面向对象的,否则就不是。我的建议是搜索 PHP 如何支持或不支持每个原则。开发人员往往过于依赖他们当前使用的语言,并且经常被误导和有偏见。举个例子,有人说 C++ 不是面向对象的编程,但却得到了很多赞许!这个人不可能离真相更远,但他的答案对许多人来说似乎是正确的!
So I did a bit of digging around and it seems that PHP5 supports these 3 principles!
所以我做了一些挖掘,似乎 PHP5 支持这 3 条原则!
回答by Piyush
Interesting Question. But "No", PHP is a Object capable Language not Object Oriented, yeah but CakePHP is an Object oriented Framework.
有趣的问题。但是“不”,PHP 是一种具有对象能力的语言,而不是面向对象的语言,是的,但 CakePHP 是一个面向对象的框架。
回答by Linus
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.
面向对象技术通常用封装、多态和继承来描述。但这些只是身份。面向对象技术要取得成功就必须强调对象。
objects-- packet containing data and procedures
对象——包含数据和过程的包
methods-- deliver service
方法——提供服务
message-- request to execute a method
message-- 请求执行一个方法
class-- template for creating objects
class-- 创建对象的模板
instance-- an object that belongs to a class
实例——属于一个类的对象
encapsulation-- information hiding supported by objects
封装——对象支持的信息隐藏
inheritance-- mechanism allowing the reuse of class specifications
继承——允许重用类规范的机制
class hierarchy-- tree structure representing inheritance relations
类层次结构——表示继承关系的树状结构
polymorphism-- to hide different implementations behind a common interface
多态性——将不同的实现隐藏在一个公共接口后面
[Alan Kays][4]["Considered by some to be the father of object-oriented programming"
] Defination:
[艾伦凯斯][4][ "Considered by some to be the father of object-oriented programming"
]定义:
EverythingIsAnObject.
Objects communicate by sending and receiving messages (in terms of objects).
Objects have their own memory (in terms of objects).
Every object is an instance of a class (which must be an object).
The class holds the shared behavior for its instances (in the form of objects in a program list)
一切都是对象。
对象通过发送和接收消息(就对象而言)进行通信。
对象有自己的内存(就对象而言)。
每个对象都是一个类的实例(它必须是一个对象)。
类保存其实例的共享行为(以程序列表中的对象形式)
An object is defined by a triple (OID, type constructor, state) where OID is the unique object identifier, type constructor is its type (such as atom, tuple, set, list, array, bag, etc.) and state is its actual value.
An object is defined by a triple (OID, type constructor, state) where OID is the unique object identifier, type constructor is its type (such as atom, tuple, set, list, array, bag, etc.) and state is its actual value.
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).
so it cannot be Object oriented in strict sense but some folk's considered it as OOP.
现在可以清楚地看到Java、C++ 和PHP 违反了规则1?为什么bcoz int, float etc. (there are a total of eight primitive types).
所以它不能是严格意义上的面向对象,但有些人认为它是OOP。
回答by Knowledge Craving
You will find all of the latest OOP features in PHP from version 5. Before PHP v5, there was PHP v4, which was not fully Object Oriented.
从第 5 版开始,您将在 PHP 中找到所有最新的 OOP 功能。在 PHP v5 之前,有 PHP v4,它不是完全面向对象的。
Also you will find many new & advanced frameworks like the following:-
您还会发现许多新的和高级的框架,如下所示:-
These frameworks have some great features & are really powerful in true sense, and some are also programmer-friendly.
这些框架有一些很棒的功能,并且在真正意义上非常强大,有些还对程序员友好。
Some notable points:-
一些值得注意的点:-
- PHP v5 still does not support Multiple Inheritance.
- PHP v5 still supports procedural way of coding, so it is still backward compatible for older websites (which had been developed in procedural way using PHP v4).
- PHP v5 仍然不支持多重继承。
- PHP v5 仍然支持程序编码方式,因此它仍然向后兼容旧网站(使用 PHP v4 以程序方式开发)。
Hope it helps.
希望能帮助到你。