I have noticed it has not been used in any of the open source PHP projects I have looked into. It wasn't used in a web programming course at my Uni. I am just curious about how much objects are used by professional PHP developers.

Using at least one object a day keeps the doctor away.
–
Linus KleenJan 7 '11 at 10:56

2

I think this is purely subjective, so no answer but a comment: 1) Good programmers use OOP when it has something to offer. PHP has a notoriously low barrier to entry, so many PHP coders have little to no formal programming training, so you can't pick a random sample and use it for conclusions. 2) It's not worth it to go OOP in PHP for small projects, for a suitable definition of "small". 3) I have taught PHP at a uni; there's only so much you can teach in a semester to students with no web programming experience. I didn't touch OOP at all either.
–
JonJan 7 '11 at 10:58

Good write up by Jon. I use OO everyday in the Zend Framework. Occasionally I'll get a client or a design agency that wants a quick fix for a contact form or a shopping basket which links to PayPal, then OO goes out of the window because the scope of project is so finite that the abstract-ness of OO isn't required
–
Jake NJan 7 '11 at 11:04

4 Answers
4

That's a very vague question, but OOP is used in the same way in PHP as it would be in any other scenario: as a way of modelling the interactions between discrete entities in your business logic.

A classic application for OOP in a web context would be a blog, where each post on the blog would be represented by an object, which in turn might have references to other objects representing the comments on that post.

As Jon said in his comment, though, OOP should be used only where it makes sense and logically fits into the system you're designing. Don't try to shoe-horn something into it just for the sake of it. Many PHP applications are simple enough not to require an OO approach, and are best implemented with a clean, straightforward procedural approach.

One caveat of OOP is that it can be quite difficult for beginners to learn, and is very easy to misunderstand. I started programming with PHP, and it took me a fair bit of trial and error to understand exactly how OOP should properly be used.

Your example doesn't really apply to php at all: the scripts runs, fetches stuff from cache/database, spits out a string and then dies. There is no living objects in memory that interact with each other because each php script starts from scratch and dies right after. At best you could fetch posts and comments as objects instead of arrays. Which is pretty much the extent of "OOP" used in PHP in addition to using classes/singletons as glorified namespaces.
–
EsailijaApr 19 '12 at 2:51

Support for OO in PHP hasn't been around forever so older open source projects might not be using it because it's based on older code.

The reason schools often teach PHP without using the OO-approach is because PHP is often the entry-language for many programmers at school. By that I mean many schools will use PHP as a language they start teaching to people with little experience with programming because PHP is easy to learn. Because object orientation seems more complex to a new programmer many courses will stay away from that and rather focus on the basics of the language. Then they will later focus on OO as part of other languages like Java. Once you learn the basics of OO from another language it's easy to combine with your PHP-knowledge and start using OO-PHP.

As for using OO-PHP it will give you a more flexible code so I'd really look into it and you'll discover it's powers in notime:)

Usually Objects are not used much in small web applications but are much more useful in larger ones. The MVC pattern is generally used. Objects are not used very frequently in php but are much more so in larger languages such as java, python and C.

I would suggest that most recent Open Source libraries in PHP do use OO principles; although older libraries may vary... and libraries written using OO are likely to survive better and be more widely used than procedural libraries.