I had a chance to have an interesting conversation with my co-workers. Most of them are flash action script or Java developers.

We talked about how well php handles OOP. I said that PHP can handle almost every OOP stuff as of PHP 5.2 or 5.3. Am I wrong? I'm not trying to get yes/no answer, but I would like to hear more opinions from developers.

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
If this question can be reworded to fit the rules in the help center, please edit the question.

@Jarrod Roberson: Languages are innate, how so? If so, please reference research supporting this claim, since I'm very interested in reading it.
–
blundersJun 8 '11 at 19:59

4

@Jarrod, there are some legitimately bad languages, particularly when they are in their early versions. Early versions of Actionscript, Javascript, Java, and PHP come to mind.
–
JordanJun 9 '11 at 0:05

2

@JarrodRoberson, so the programmers that designed PHP...
–
dan_waterworthJan 1 '13 at 10:06

8 Answers
8

The problem with PHP in respect to OOP is, that OOP was really just bolted on to the language, whereas in languages like Java and ActionScript it is part of the core concept, although I consider both bad OOP languages because of their poor object semantics.

Even though PHP nowadays is largely suitable for OOP, the problem is, that:

the vast majority of the code base out there (libraries, frameworks, custom code) is not written in PHP 5.3

the vast majority of the API is procedural. array is still the most important and most used type and it is not an object.

the vast majority of the PHP programmers will just stick with procedural style in the foreseeable future. PHP doesn't promote OOP, as opposed to Objective-C for example: Objective-C has C as a full subset and thereby allows procedural programming in one of its purest forms, but it clearly favors the use of OOP.

PHP cannot simply get rid of all the legacy stuff. If a PHP 6 were released, that throws most of it out, it probably wouldn't be used. It would defeat its purpose.
And PHP failed to take on a sufficient pace of gradually deprecating things. So it will take some time until it gets the necessary clearness and consistency a good language should have.

Personally, I really dislike PHP, simply because of all that baggage, the API and everything.
However frameworks as Flow3, Symfony, CakePHP and Codeigniter provide a solid basis for OOP and other powerful paradigms. Due to ActionScript's and Java's restrictedness, with sufficient effort (an abstraction layer atop PHP flaws), PHP can be on par or even outrival them.

To summarize: There's nothing particularly wrong with PHP's OOP facilities. Therefore you could say it's not a bad OO language. However there's a lot of things particularly wrong with PHP, such as the OOP facilities not really being integrated, but rather just included, therefore you could argue it's a bad OO language.

Out of curiosity, could you explain why you think Java is a bad OOP?
–
dkuntz2May 21 '11 at 15:41

2

Ahem.. I might update your answer to be specific to Actionscript 3. Prior to that, Actionscript was not designed to be OO (being ECMA-based) and even Actionscript 2 was simply a wrapper for AS1.
–
Demian BrechtMay 21 '11 at 20:16

You'll find that "can handle" is not the same as "supports". I mean, down at the nitty-gritties, even C "can handle" OOP if you structure your code properly. The question is whether or not the language goes beyond merely enabling OOP into encouraging it by making OOP the most natural way to program.

In my (admittedly limited) experience the PHP 5.n dialects do not do this. It's too easy to slip out of OOP and into pretty much purely procedural code. (Note that I don't think PHP is a bad language for this reason. There are many reasons why I think PHP is a bad language, but the OOP support is not one of them. ;))

Man, I could go on for ages about that, Moon. Here's a snippet of code that fails: $e = function_that_returns_an_array()[0];. Here's what you have to do to fix it: $a = function_that_returns_an_array(); $e = $a[0];. Languages whose syntax won't let you directly use the result of a function call tick me off. Also, tell me what the following number is in decimal: 0246875. (Hint: you will not be able to find a dumber way to implement that lexer!) PHP is just chock full of this kind of stupidity.
–
JUST MY correct OPINIONMay 21 '11 at 10:34

6

@Moon Here is some reading for you: softwarebashing.org/blog/2009/09/php-the-ultimate-suck and tommorris.org/wiki/PHP%20Sucks There are some serious problems, but a lot of it is just nit picking. What I really hate about PHP is the culture, it's simply not as professional or academic as the alternatives, namely ruby and python. The alternatives are nicer and they attract the best programmers. For example ruby and python both have interactive shells, decent functional features and clean syntax. They are a lot more fun to use. Besides market share in shared web hosting why use php?
–
KeyoMay 21 '11 at 12:33

7

"There are only two kinds of languages: the ones people complain about and the ones nobody uses." Bjarne Stroustrup. Meditate.
–
SylverdragMay 21 '11 at 15:45

There are really two definitions of a language's object orientedness: how object oriented is its own built in syntax and standard libraries, and what influence does it have on the software programmers write for it.

By the first definition PHP seems to be toward the bottom of the list. People often cite non-object strings and arrays as evidence of this. In my opinion, this definition doesn't really matter. You can always wrap it in an object if you really need one, but people don't because they don't. Usually the only difference it will make to code is changing function(var) to var.function(). Syntax alone doesn't change something from non-OOP to OOP.

As for the second definition, people manage to write poorly object oriented code even in languages that strongly enforce such constructs, and people that write good object oriented code are barely affected by language at all, except for being annoyed by syntactic quirks. In other words, in my experience, there are no bad object oriented languages, just bad object oriented programmers. PHP is as good as any other language in that regard.

Maybe some languages are better suited for learning object oriented programming, but I think that will differ by developer. For me, it didn't click until I read Larry Wall's camel book about how perl does (did?) OOP. Having to explicitly bless references as belonging to a class really drove home the point to me of what an instance of an object really is versus what a class is. Some people favor Java's all-objects-all-the-time approach for learning. Since OOP is more an architectural concern, it's easier to learn after you know basic variables, expressions, sequence, selection, and iteration, so any language that doesn't foist OOP on you right away has an educational advantage in my opinion.

When my wife took an intro to programming class with java, she was constantly frustrated by the public static void main and putting everything in a class, which she didn't have the background yet to understand, but was told by her teacher just to trust she needed it. I tried to explain it, but it's very hard to explain to someone who just barely learned about variables why it would be useful to prevent other parts of the code from accessing them, and how to decide how to split it up. You might argue that learning procedural programming first inculcates bad habits, but what about the habit of copying and pasting code you don't understand?

It seems the terms "object", "language", "oriented" and "programming" have not been agreed upon and are ambiguous and relative so no possible black or white answer is possible. But again "black" and "white" are not agreed upon and their meaning is elusive, etc. etc. etc
–
user61852Jan 1 '13 at 5:02

PHP isn't bad for object oriented programming. However, the nature of PHP encourages quick hacks and fixes versus proper object oriented software development, and many books and tutorials ignore OOP concepts entirely. It can support OOP concepts just fine, but it's the onus of the developer to know to apply them. You can do most anything in PHP that you could do in a "true" OOP language like Java or C#, but those languages have more enforcement of OOP techniques than PHP, which could be used procedural style if you chose to.

I can't remember the exact quote but it was from someone comparing raw PHP to using the then-new Ruby on Rails, and went something like this: Rails makes writing good code easy, and writing bad code hard. PHP makes writing bad code easy and makes writing good code hard. The line about PHP pretty much sums it up on OOP; it's perfectly capable of being a good OO language, but it makes it a little more difficult to do so.

PHP Categorized

PHP is only a glue language just like BASH or Perl. It is good at that, but not good at anything else, leave apart some serious work. The language is not designed. It is merely evolved by hacking up various code together in a haphazard manner (code-and-fix).

Compiled Languages

In contrast to PHP, Java is a compiled language which has been properly engineered. There are JSRs defining the language, many enterprise-grade frameworks and concepts such as EJB, JMS, ESB, Spring, Struts, Hibernate, and other.

Enterprise Software

In terms of enterprise systems, Java EE is a solution which fits the purpose (Enterprise Edition) whereas PHP is used in companies that are trying to reduce costs by hiring a cheap labor with lesser qualifications.

There has been a considerable effort to drag PHP into the Enterprise segment using various frameworks. Most notably, Zend Framework 2. The fundamental problem here is not object-orientation of PHP, but it is the lack of design, missing strong typing, non-standard solutions to standard problems (kind of hacks for everything), and a complete lack of any prescribed architecture.

Software Design (Architecture Discussed)

With PHP, the burden of architecting software is still fully up to the developers who have been doing a very poor job i.e. often not having any architecture at all, just randomly code and fix. Security and transactions are missing and need to be shoehorned by developers. In Java, one solution is annotated EJB. Also consider the fact that, in PHP, nothing happens if you omit catching exceptions or make various errors. That is until the run-time. With Java, you will get warning and errors directly at design time. That is called robustness, but with PHP you can only keep on dreaming.

Multithreading

PHP does not support multithreading. The code is always a single thread. This hinders its performance for non-trivial problems under heavier load. With Java EE, multithreading is fully supported, for example via the Runnable interface.

Support and Standards

Also consider the deployment, Web Services, and other standards. Whereas in Java, there are big companies like Oracle, IBM, RedHat, Apache, and many others, PHP has only Zend.

Conclusion

To conclude, PHP is a very bad object-oriented language. Strictly speaking, it is not even object-oriented, but hybrid which is bad from versions >5 because OOP is mixed up with procedural programming. I would only recommend PHP as a glue like BASH, but for serious work I'd use Java EE.

Related Thoughts

The main deal with the latest Zend Framework 2 is that it is trying to be like Java EE, but completely fails to deliver at least a remotely comparable set of available packages, features, tools, automation, error-checking, architecture, design, and everything.

It is, in my experience, more expensive to use PHP for complex projects than with Java.

There are also some rumors such as that PHP stands for Pretty Horrible Programming. I can confirm these.

How well does a language handle OOP? I would rather ask how well can i write a program in an OO way. I can thumb my nose at everything-should-be-a-class stance taken by Java by making everything public static.
PHP supports OOP; it doesn't force me to write in OO manner only. How well does it handle depends on how well i understand and write the program in an object oriented way.

Well we have to think of what makes a language more OO and what reasons are there that made it like that.

JavaScript is an implementation of ECMAScript which meant to run in browser environement as an Interpreted language. The fact that it is thought to be interpreted language had very big effect on its syntactical/behavioral design.

For example, it does not follow OOP. But apart from that there are many facts like, OO prgrammer may find some of its behavior like function hoisting very confusing.

Again there are many things came with OO languages like C++, Java, C# to make them compiler efficient like strong typing. However, since JS runs in interpreted environment it does not follow strong typing instead it is loosely typed language.

Apart from above behavioral differences there are many syntactical differences like JS have Object Literal notation that may very well confuse C# programmers. However C# do also have Object literal like syntax, even though it is compiled language and such syntax is rarely used as it is not traditionally OO code style.

Now there is one more point that dictates whether the language is good OO: is it evolved from C++. As Java, C# are evolved from C++ and follow similar behavior and syntax, a big community does perceive such behavior and syntax as the only OO thing, and think that any language that does not inhibit such similarities are simply not OO.

However let us not forget that OO is very abstract concept it is not tied to any syntax style and even not to any specific beahavioral property.

And PHP is very well OO. Simply not looking and feeling like Java, C++, C# does not make it poor OO language. Well I learnt C++ then Java and then C#.

So, until now my head was very well OO. Then I learnt JS from a very good book "Wrox Pro", and it just overblown me. I just enjoyed the behavior and syntactical distinctness of JS. Then I realize that Object literal like syntax was in their in C#. And now while learning PHP I am feeling as if it brings many things from both worlds.

All we really have to do is to learn syntax and behavior subtleties a language have while implementing OO. Once we master them we may start think that this is better OO implementation.