Argh! PHP is *NOT* “object oriented!
Goodness. I like PHP. I use it a lot. I’ve been using it since early 1996. I’m Zend Certified, have done billion dollar ecommerce projects in PHP, and like to think I know a little bit about PHP.
It is *NOT* “object oriented”. I wish people would quit saying it. Perhaps there’s a need to impress non PHP people, or to try to get across the idea that PHP supports objects. Maybe that’s fair, but I don’t think so.
I remember talking with people in 2004 about PHP5. They were using it in beta, and “loved” how it was “object oriented”. I asked why they were using PHP5 in production when it wasn’t even released yet, and it was “PHP4 doesn’t support objects”.
As my wife would say (if she cared about PHP at all) – “Bollocks”.
Yes, we’re years in to PHP5, but I still come across people who state that “PHP is object oriented” or “Since version 5, PHP is object oriented”.
DEAD.
WRONG.
Look at the words “object oriented”. Is the language really “oriented” around or towards “objects”? Abso-smurf-ly not.
Groovy is object oriented. Pretty much everything in the language is an object or can be treated like one (objects, functions, “primitives”, etc.)
Ruby is object oriented. I’ve not done much with it, but as far as I can tell, everything is an object (numbers, etc.).
Java – again, with limited exceptions, everything is an object.
PHP – very few built-in things are objects (dir?). The SPL is perhaps making an attempt in that direction, but they’re not looking to replace PHP, only be an adjunct. Strings, arrays, numbers – nothing is an object, nor can it be treated as such.
So, why do we continue to say “object oriented”? I don’t. I say “object capable”. I get weird looks, and occasionally some reactions that are uncalled for, but I still maintain – PHP is not “object oriented”. And likely it shouldn’t be. Given that we’ve come this far without it, overhauling PHP so that everything is an object should only be done at a major version. PHP6 is not that version, so we’ll have years more before/if we ever see a true “object oriented” PHP.
But that doesn’t matter. Revel in the fact that PHP is a hybrid that supports object-based development when necessary, but allows you the flexibility to be procedural when the time is right. Wear it as a badge of honor. But don’t cop out and try to claim that PHP is “object oriented” when it’s not.
*Arguably* one could say PHP permits “object oriented” development, to the extent that one orients their development around objects and thinking of objects. Possibly fair point, but it’s an niche distinctions, and one which likely few people really *mean* when they say “PHP is object oriented”. What people really mean when they say “PHP is object oriented” is “PHP is object capable”. So just say that instead.
Over and out…

Reason number 1: it doesn’t not support casting of objects from one class to another. I know, I had to create the function for php-qt so that you can downward cast regular Event objects into specific mouseEvent objects as per the usual in any “OOP” language. This lessens the pillar of polymorphism in the language. This is mostly because the ZE fails to use classes in any meaningful way other than simply responding to get_class() calls and looking up parent methods.
It has support for objects, but they are a complete after-thought and will only continue to get hacked-on support until ZE promotes class types up to the zval struct and starts treating type comparisons at the class level instead of just at the Object vs string vs int level.
http://websvn.kde.org/trunk/KDE/kdebindings/php/phpqt/src/functions.cpp?r1=875761&r2=883060&pathrev=883060
I’m cool with Object Capable. I actually like the idea of a ‘hybrid’ language. Maybe I’m just too much of a hippie. I mean I’ve got Nag Champa burning. I’m wearing shoes made of hemp and recycled water bottles. And I drive a moped. Plus, I like things to be different. I like to spell colour and favourite the Brittish way. I like to say ‘from the get set’ vs ‘from the get go’. I’m not usually into the popular or trendy thing. So, PHP OCP it is.
You HIPPIE! We’d missing you in class today!
Hemp shoes? Recycling? PHP? How is that not popular nor trendy?
Trendy among a small demographic. I don’t have time or patience to make my own shoes out of my own organically grown materials. I just prefer to opt for either the path less taken or the path of less politically driven economics from greedy corporate giants. But, I suppose that’s trendy too. You got me.
The thing that bothers me about PHP is that while it may be “object capable”, it still lets you get away with all kinds of hacks. On a big, very complex project with a diverse team containing junior developers, this can become a problem.
Almost any language lets you get away with ‘hacks’, but just different kinds of hacks. Things that people accept as “cool” at first may be thought of as “hacks” later on, even by the same people.
Not sure what sort of ‘hacks’ you’re talking about wrt PHP and objects.
I typically use the term “object oriented PHP” to differentiate from procedural usage. I also like “object capable”, but it has less connotation of “OOP” for me.
semantics? Who cares..
Evert – are you anti-semantic?
The trouble with using the term is that it has a fairly established meaning and set of baggage with it that PHP basically does not adhere to (all/most aspects of the language being rooted in objects). When using the term with people from other languages (teaching, or working on projects together) there’s a mental shift that people need to bridge, and using terms that obviously don’t have the same meaning as they do in other platforms simply muddies the issue and makes it that much more time consuming for someone to get up to speed.
@matthew – “object oriented PHP” is a good way to describe the style of particular code. Yeah, I can see that being used appropriately – that’s good.
“I made up the term “object-oriented”, and I can tell you I did not have C++ in mind. ” — Alan Kay
If you’re using ‘everything is an object’ as the acid test for an OO language, you can throw out C++ and pre-autoboxing (1.5?) Java as well – they both have non-object primitives. This battle was lost decades ago by fans of Smalltalk trying to convince people that C++ was not OO.
Given that we’re talking about PHP, any discussion of language purity is going to end up with “you’re right, in a pedantic way, but nobody really cares”.
I am taking things other way around. What is OOP really – It’s all about the fundamental concepts of OOP – Class, Object, Instance, Method, Method passing, Inheritance, Abstraction, Encapsulation, Polymorphism, Decoupling (yes, I did copy/paste those from wikipedia).
If programming language provide me tools to apply those concepts I would consider it OOP. I don’t really need to “string”.getLength() or whatever and have all object to consider it OOP… it’s the mental state I care about and what you can ultimately achieve using OOP. No really surprised you get “weird looks”.
Cheers
I agree with you, PHP can be use object mixed procedure. It makes PHP flexible to write in many situations, the difference from Java and Ruby.
Strictly speaking, no widely used language is pure OO. Java has primitive types, Ruby, Python and C++ have procedural possibilities, etc.
Maybe C#, but I don’t know it enough.
PHP implements all OOP concepts (classes, objects, instances, methods/message passing, inheritance, abstraction, encapsulation, polymorphism), so, IMHO, is OO.
PHP Object Oriented vs PHP Object Capable.
That’s kinda like saying alt tag vs alt attribute.
Yes, one is ‘right’, but, does it really matter?
Oh wait, we’re all programmers, of course if matters.
I’m totally ok with Object Capable and absolutely agree to say that PHP is not Object Oriented. My reason ?
The big problem with PHP -as- Object is when something is entirely made with classes (like these today’s frameworks), for each retrieved page, everything is reloaded in the memory, the basic framework, the needed classes (modules, plugins, whatever), etc
How could this be powerful ? It’s so heavy. Take a look at the comparisons made by Rasmus Lerdorf about Frameworks performances !
For example, Java frameworks (Struts, JavaServer Faces, etc) stay loaded in the memory and aren’t instanciated for each page accessed.
When PHP will act like this, we would have a better implementation of doing things in OOP approach.
I personally use the phrase “object-structured” for my own code, precisely because it is not fully-OO but merely packaged into classes and objects. On the exterior there are objects (portraying some application structure), but internally there are mostly base types and some function calls.
And I don’t turn ALL code into methods. Sometimes it just looks nicer to keep procedural code, you know, procedural.
I would agree to Michael.
PHP is not object oriented.
It supports OOP but not necessarily an OOP language.
Put simply, OOP in PHP.
A lot of object-oriented development is done in C++ and yet the basic types in that language (int, char, pointer) aren’t objects. Turning them into objects wouldn’t make the language better, or make developing with it faster or easier. Objects are far more useful at the framework level than at the language level. For example, there’s loads of C++ string classes build for slightly different purposes (std, CString, etc). Having strings as objects at the language level would mean a one-size-fits-all string implementation that obviously wouldn’t quite fit everywhere.
PHP is NOT OO, and what kills me is these HUGE frameworks that use total OO to take up time, memory and resources to display a simple page. I have seen 5 page websites built with huge OO framworks… WHY….because todays programmers are lazy.TOO afraid to take the time to write some good ol solid code. They become dependent on these Frameworks as their lifeline like welfare recipiants to the Government. AND then they have the audacity to call themself a PHP master. Drop these folks into a non-framwork environment and they are lost. In my opinion, if you cant write a framework from scratch, then you have not business using one.
Rants over….long live PHP
I completely agree with you… but still, I love php
You know, you’re absolutely right, and I have been struggling with this information for a long time. Let me explain.
I am relatively new to PHP programming (started about a year to a year-and-a-half ago). All of that time I struggled with learning how to program in an object-oriented way in PHP. I searched and I searched for the answers, the books, and the tutorials.
But, until I read this article, I had been doing it wrong the whole time. I kept trying to find that secret that would turn me into a PHP OOP (object-oriented programmer), but no secret was to be found. So, I started playing around with it, trying to develop a technique, and developed a technique that I liked. Over time, I started developing objects on my own that were very modular and could be extended easily. Then I read this article and it “clicked”. It’s not that PHP is an object-oriented language, it’s that it is capable of being one if that’s what you want. It’s all a matter of the environment you build your application to exist within.
I turn my procedures into methods when it is necessary for it to be modular. If it’s only going to be used one time, I do it procedurally and document it so I can find it again (if times change and it needs to turn into a modular method).
Nice write up.
Nice write-up! I got into an argument about his once… had to prove it with code….
$language = 'PHP';
$PHP->rocks(); // but is not OO
The project I am working on now we’re lucky enough to have the time to create a set of helper classes that force PHP into being an OOP language. Basically it’s just creating a bunch of type-object and using them instead of PHP’s own scalar types. Of course this is noly sane if your design demands it… From our perspective it’s using the “hybrid” part of PHP to the max… everything in the public API is an object, everything else is basically the developers choice.
Best of both worlds.
(Oh, and of course we’ve got Unittests up the wazoo, just to be safe…;-)
thanks for this site i have cleared my doubt as to whether PHP is OOP or not…
Thanks a lot!!
you obviously don’t know anything about OOP. It’s not about having just objects, it’s about Polymorphism, Inheritance, etc. You need to really start coding in an OOP language or else you’re never going to understand that. So yes, PHP is NOT OOP.
Hrm – anon. I guess I don’t understand your point. I said PHP was not object oriented, and you seem to be agreeing, but wanting to disagree. ???
If we use object oriented Framework we can make PHP as object oriented for example consider NOLOH it is an object oriented PHP framework. The NOLOH objects can be extended very easily. So it is completely flexible. NOLOH supports automatic error-handling. The syntax is easy to understand. Any PHP programmers can easily work with the NOLOH syntax. It contains fully customizable controls from basic to complex ones. It provides Automatic State Management. It manages all applications and user view state for the developer automatically. It also maintains a smooth communication between client and server. In NOLOH you can use any types of coding tools or different libraries. It is not like a group of extensions and application.
nihdalas – I’m not sure what you’re getting at, beyond some slight promotion for NOLOH (link is noloh.com btw!). Nothing in noloh makes PHP *the language* “object oriented”. I still maintain PHP is “object capable”, and really that’s fine. It was/is just the term “object oriented” applied to the language which I object to. Frameworks may be “oriented” around “objects” – fine, but the language isn’t.
I like the freedom PHP gives me when choosing between OOP and procedural approach. For simple scripts and projects, there is absolutely no need for OOP, and procedural is pretty straight-forward. For more complex work, I like to go the object-oriented way. All in all, this article tells the truth. PHP is not an OO language – and there is no need for it. Just use the language the right way and you gonna be fine.
Nice clarification Michael! In my experience, I have built two web apps, one using functions and the other one using the PHP OOP capability (e.g. using classes), the first one is much faster and use less memory.
What about PHP 5 (and 6) CAN be used in an object-oriented manner?
The objects can be. Strings, Arrays, Integers, Floats and such are not objects, and can’t be dealt with as objects. Most of PHP is still function-oriented, and that will continue to be that way probably forever. And that’s not bad. It’s just that the language is not object oriented. Your *apps* might be considered OO, if the app was oriented around objects.