49

Is PHP an object-oriented language? If not, then what about the framework CakePHP? Is it an object-oriented MVC implementation of PHP?

Also, can a PHP application wholly built using classes be called object-oriented?

Archy Will He 何魏奇
  • 9,589
  • 4
  • 34
  • 50
avon_verma
  • 1,229
  • 3
  • 12
  • 17
  • 1
    Keyword is mostly, because PHP is not completely OO (if it were, there wouldn't be any raw scalar types for example). – BoltClock Jan 15 '11 at 12:21
  • Multiple inheritance is still not there in PHP So it not fully Object Oriented. – Shakti Singh Jan 15 '11 at 12:26
  • 2
    If the basic language semantics allow for both paradigms, then it's commonly classified as an *hybrid* language. – mario Jan 15 '11 at 12:32
  • 5
    @Shakti Singh. Multiple inheritance is not a basic concept of OO. – GolezTrol Jan 15 '11 at 12:39
  • 1
    @Shakti Singh: Then Java is not OO either. – Felix Kling Jan 15 '11 at 12:49
  • posible dupe? http://stackoverflow.com/questions/3895033/why-is-php-considered-object-oriented – Trufa Jan 26 '11 at 14:07
  • @Framework it has implemented traits in place of multiple inheritance. It is a valid alternative to still be considered and OOP language. Besides the design choice of traits/mixins over plain old multiple inheritance is normally considered the better choice nowadays. People are starting to realize that with all the things that OOP brings to the table inheritance is the one thing that isn't really a good idea. – ddelrio1986 Sep 17 '15 at 02:10

7 Answers7

108

No, PHP is not fully object oriented language.

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).

PHP is, what one could call, "object-capable language".

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.

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.

Marcos Dimitrio
  • 6,651
  • 5
  • 38
  • 62
tereško
  • 58,060
  • 25
  • 98
  • 150
  • 35
    +100 for `just because you are using classes, it does not make it OOP`, I have never seen "OOP" PHP code that wasn't just using classes as glorified namespaces. More people need to realize this! – Esailija Apr 19 '12 at 02:16
  • @Esailija *singletons* are glorified namespaces, as are static classes, and I can see where you are going with the 'primitives' argument. But how are regular object instances in PHP different from object instances in C# or Ruby? – GolezTrol Apr 26 '13 at 06:04
  • @GolezTrol Because of the runtime environment of PHP, you rarely need more than one instance of object in a given execution, reducing it to a singleton that can be instantiated more than once but never makes sense to. If you have domain object model, then those are not any different. – Esailija Apr 26 '13 at 09:17
  • @GolezTrol Thing is, in PHP, it's so easy to just use stdClass/Array directly from database results so many don't ever create an object model in PHP that would make it necessary to instantiate a class multiple times in a single PHP execution instance. – Esailija Apr 26 '13 at 09:43
  • 4
    @Esailija Your first argument is about the structure of an application. The fact that you only need one instance most of the time doesn't mean it's not OOP. It depends on the application structure, and that's not really different from, say, Ruby. If you have a webshop, for instance, then a list of products will probably translate to a list of product instances. – GolezTrol Apr 26 '13 at 09:54
  • @GolezTrol yes, if you have a class for a Product, then you are doing object modeling for your domain, and in that case there is no difference like I said. – Esailija Apr 26 '13 at 09:55
  • 14
    You are placing OO on a cloud, and are placing extremely strict requirements on what wears the OO medallion. "Hmm this code is using objects.. but it's not object oriented. Nope." Chill. – bobobobo Aug 19 '13 at 01:37
  • 9
    @bobobobo ain't you confusing object-oriented languages with object-oriented programming? One addresses structure of language, other - a paradigm in programming. You can adhere to OOP paradigm in languages that do not have classes (like - javascript). You can even do perfectly fine OOP in languages that do not have objects (like in Asm or Erlang, or ANSI C). PHP as language is **not object-oriented**. That does not mean that you cannot use OOP in it. Hell .. you can use it as well as in Java. Chill `=P` – tereško Aug 19 '13 at 10:19
  • 2
    It seems like if you are able to do certain things: create "classes" and "inheritance" even if you have to hack it together, it means that you can use program using object oriented concepts...That's really the end goal; to create code that is robust yet efficient, without reusing and making it as simple as possible to update especially as you add complexity. Whether or not it FORCES you to constrain yourself to objects is less the point as to whether it ALLOWS you to create an object like environment. – Snowburnt Aug 19 '13 at 12:13
  • 1
    With regards to: "(the) Only pure object-oriented language(s), that i know of, are Ruby and Scala..." I think this is possibly taking things to a somewhat purist extreme. Java and C# both have object orientation as their principle structure. You build C# and Java code within classes. Just because their is the possibility to use those languages in a somewhat procedural sense does not mean that the overall aim is not OO. As far as people are concerned they are OO programming languages. – Cheesus Toast Apr 01 '14 at 18:54
  • 1
    @Tereško - Hardly... you are quibbling over semantics. It is like saying HTML is not a programming language (language that provides coded instructions to a machine); it is a similar issue. Definitions within computer science need not be so complex. If people concentrate less on making things more convoluted then it will not be so difficult for people to learn. Certain aspects within computer science need not be made into philosophical issues. OO should be considered nothing more than a design pattern. – Cheesus Toast May 02 '14 at 16:10
  • 1
    Object oriented, procedural, functional, aspect oriented are paradigms and **not design patterns**. You probably need to look up what "design pattern" is, before you used tat term again, otherwise you will look quite silly. And, btw, asking *"is language X adherent to programming paradigm Y"* itself **IS** a question about semantics. And it won't change just because you don't like the answer. – tereško May 02 '14 at 22:45
  • 1
    @Tereško - Agreed, I have used the wrong terminology (and I should have checked it before adding comment). There are differing views on the contextual use of "Object Oriented". I respect your viewpoint; I do not necessarily agree with it. – Cheesus Toast May 03 '14 at 15:13
50

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).

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.

GolezTrol
  • 114,394
  • 18
  • 182
  • 210
  • 3
    Update your question? http://michaelkimsal.com/blog/php-is-not-object-oriented/ I'd like to see your thoughts. – Keyne Viana Mar 28 '12 at 23:34
  • 4
    It's not my question, but it is my answer, which I won't update. :) But here's my comment: I think it's all about semantics, about your definition of object oriented. If you say that everything in a language should be an object befor you can call it object oriented, then PHP is not an OO language. But to me and many others, it only makes sense that "An OO language, is a language that allows OO programming". Basis concepts of OO are generally considered to be Dynamic Dispatch, Abstraction, Inheritance and Encapsulation. PHP implements all of these, and some more. – GolezTrol Mar 29 '12 at 06:03
  • I'm a Delphi programmer myself and for Delphi, there's the same discussion. Delphi supports classes with all those concepts mentioned above, but, having evolved from Pascal, it also supports a procedural style of programming and many functions, including the imported Windows API functions, are just functions/procedures. Nevertheless, there's big support for objects, and large parts of the application framework (VCL) is object oriented. I call Delphi an OO language by the same definition as PHP. – GolezTrol Mar 29 '12 at 06:09
  • Not only does PHP support OOP, but many of it's functions (like MySQL functions) are available in both procedural and OO style, so you can decide which one to choose. That way, when you use a framework like CakePHP, you can have an application structure that is OO to a great extent, except maybe operations on simple types, like ints and strings. But as the answer by @tereško tells you, even Java has these primitive types... – GolezTrol Mar 29 '12 at 06:10
  • But because there's so much discussion over the definition of OO that I've written more than just 'Yes' in my answer. And now there's this additional comment as well, in which I hope you see my thoughts. :) – GolezTrol Mar 29 '12 at 06:12
  • 2
    Yes. I'm meant your answer (mistake) =) Semantics matter though, and to be semantically correct, you need to choose the right words, so that the logic can be traced without ambiguity. The PHP language itself is not object oriented. The Ruby language is object oriented, so is Java with exception of the primitive types. PHP is a hybrid language capable of objects creation, so is Delphi. There is a big semantic difference between a hybrid language and a object oriented language. It's not the ability to create objects, but the fact that the language itself is/isn't object oriented. – Keyne Viana Mar 29 '12 at 16:10
  • That is *your* definition, though admittedly, you're not the only one. But neither was I just making up my answer and my opinion is carried by many others, which is only proven by the upvotes to my answer. But if you disagree, you can choose to upvote another answer, add one yourself, or edit this page: http://en.wikipedia.org/wiki/List_of_programming_languages_by_category#Object-oriented_class-based_languages But anyhow, I'm not changing my answer nor my opinion because you found a random blog post that disagrees with me. :) – GolezTrol Mar 29 '12 at 18:38
  • 1
    No worries, I was justing throwing some arguments to see your thoughts, and posted this last comment in regards of what you defined as semantics. Anyways, thank you for your attention. – Keyne Viana Mar 29 '12 at 18:46
3

PHP is not fully object oriented but it supports some feature like

1) class
2) object
3) Constructors and Destructors
4) Object Inheritance
5) Scope Resolution Operator (::)

and many more. If you want to learn object oriented php refer the below reference link

1) http://php.net/manual/en/language.oop5.php

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
CodePlateau
  • 381
  • 2
  • 5
  • 18
3

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!

So I did a bit of digging around and it seems that PHP5 supports these 3 principles!

3

Interesting Question. But "No", PHP is a Object capable Language not Object Oriented, yeah but CakePHP is an Object oriented Framework.

Piyush
  • 35
  • 1
  • 5
2

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

class -- template for creating objects

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:

  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)

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.

Linus
  • 899
  • 3
  • 19
  • 33
1

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.

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).

Hope it helps.

Knowledge Craving
  • 7,955
  • 13
  • 49
  • 92