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?

7
  • 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, 2011 at 12:21
  • Multiple inheritance is still not there in PHP So it not fully Object Oriented. Jan 15, 2011 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, 2011 at 12:32
  • 5
    @Shakti Singh. Multiple inheritance is not a basic concept of OO.
    – GolezTrol
    Jan 15, 2011 at 12:39
  • 1
    @Shakti Singh: Then Java is not OO either. Jan 15, 2011 at 12:49

7 Answers 7

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.

13
  • 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, 2012 at 2:16
  • 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, 2013 at 9:54
  • 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, 2013 at 1: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, 2013 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, 2013 at 12: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.

8
  • 3
    Update your question? michaelkimsal.com/blog/php-is-not-object-oriented I'd like to see your thoughts. Mar 28, 2012 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, 2012 at 6: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, 2012 at 6:09
  • 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. Mar 29, 2012 at 16:10
  • 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. Mar 29, 2012 at 18:46
4

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

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.

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.

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.