Is PHP Object-oriented?

56,356

Solution 1

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.

Solution 2

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.

Solution 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

Solution 4

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!

Solution 5

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.

Share:
56,356

Related videos on Youtube

avon_verma
Author by

avon_verma

Updated on July 09, 2022

Comments

  • avon_verma
    avon_verma almost 2 years

    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?

    • BoltClock
      BoltClock over 13 years
      Keyword is mostly, because PHP is not completely OO (if it were, there wouldn't be any raw scalar types for example).
    • Shakti Singh
      Shakti Singh over 13 years
      Multiple inheritance is still not there in PHP So it not fully Object Oriented.
    • mario
      mario over 13 years
      If the basic language semantics allow for both paradigms, then it's commonly classified as an hybrid language.
    • GolezTrol
      GolezTrol over 13 years
      @Shakti Singh. Multiple inheritance is not a basic concept of OO.
    • Felix Kling
      Felix Kling over 13 years
      @Shakti Singh: Then Java is not OO either.
    • Trufa
      Trufa about 13 years
    • ddelrio1986
      ddelrio1986 over 8 years
      @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.
  • Keyne Viana
    Keyne Viana about 12 years
    Update your question? michaelkimsal.com/blog/php-is-not-object-oriented I'd like to see your thoughts.
  • GolezTrol
    GolezTrol about 12 years
    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
    GolezTrol about 12 years
    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
    GolezTrol about 12 years
    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
    GolezTrol about 12 years
    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. :)
  • Keyne Viana
    Keyne Viana about 12 years
    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.
  • GolezTrol
    GolezTrol about 12 years
    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: en.wikipedia.org/wiki/… But anyhow, I'm not changing my answer nor my opinion because you found a random blog post that disagrees with me. :)
  • Keyne Viana
    Keyne Viana about 12 years
    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.
  • Esailija
    Esailija about 12 years
    +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!
  • GolezTrol
    GolezTrol almost 11 years
    @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?
  • Esailija
    Esailija almost 11 years
    @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
    Esailija almost 11 years
    @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.
  • GolezTrol
    GolezTrol almost 11 years
    @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.
  • Esailija
    Esailija almost 11 years
    @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.
  • bobobobo
    bobobobo over 10 years
    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.
  • tereško
    tereško over 10 years
    @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
  • Snowburnt
    Snowburnt over 10 years
    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.
  • Cheesus Toast
    Cheesus Toast about 10 years
    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
    Cheesus Toast almost 10 years
    @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.
  • tereško
    tereško almost 10 years
    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.
  • Cheesus Toast
    Cheesus Toast almost 10 years
    @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.