views:

196

answers:

4

This is part observation, part question.
First the observation:
While everyone talks of modular programming, OOP, procedural done right, aspect oriented, design patterns, etc., several popular opensource PHP apps are plain script files with the structure being controlled by includes and requires.

This looked stupid to me till I faced a recent issue with my shared web host - they don't support MySQL stored procedures on shared hosting. I checked out many competing shared hosting packages - same story.
Then I rewrote the code using SQL queries and some static functions in DB-handling class.
That was when I realised that the said PHP projects actually take into account the whole spectrum of web hosting packages and so decide to keep the code as dumb as possible, so as to reach a wider user base.
The other thing is the reach of the script to newbies without a formal Software Engg background: Scripts are easier to hack for newbies.

Those two were the good reasons I saw to explain the phenomenon.
Make no mistake, the guys maintaining these projects are pretty good at software development, so it is not incompetence.
Sometimes they have spare cash too.

Now the question: What other sensible reasons can you think of?


EDIT: I personally feel that it's not about OOP alone, as pointed out by others, good code structure is not dependent on OOP/Procedural style. I've seen and coded a few functions-based PHP projects myself.

The thing that bothers me most is that the folder/filesystem layout is good, the file/folder naming is great, documentation is plenty, standards are followed, but, when you open the file to read the code, there's a hundred if-then-else conditions, version-checking, odd use of output buffering here and there, cookie manipulation code, some constants, includes, and no clear structure in many files.

At least I seem to be lost every time I try reading the code. But if I read code from Java or C# code bases, or even some other sidelined PHP apps - there is proper separation of code inside functions, templating is used for display and so on. Looks organised. Looks easily understandable.
Backward compatibility could be the issue for maintainers, but they would be open to making the next version in a more structured way. But that too does not happen!
Obviously, I'm missing something because those maintainers always work hard, after all.

+3  A: 

Provider compatibility is an issue in many areas when building applications with PHP, it's not the reason why OOP is not used in certain projects.

Several Object Oriented Programming characteristics like interfaces, the public/private/protected keywords and more can be only found in PHP 5. Some applications still support PHP 4, mainly for the reason that there are still providers out there who won't upgrade (out of the justified fear that their clients' PHP 4 apps will crash by the dozen). Therefore, there is still a lot of "primitive" PHP 4 OOP code around. But there is no living PHP version that does not support at least basic OOP.

include and require are used to import code snippets into the current script. You will find them in object oriented applications as well.

There are several software products that make almost no use of OOP at all, and that's fine as long as the overall code quality is in order. While it is regarded by many (myself included) as a important method to write better, more reusable software, OOP is no requirement to write good software.

Pekka
Obviously OOP does not make a software any good. But can you call "this software is OO" or "this programming language is OO". A kernel written with C cannot be OO but it can be perfect because C is structured. On the other hand missing object oriented approach in an Object Oriented Programming language causes a good software? I don't think so.
JCasso
I'm not sure I understand you. What I am trying to point out that OO is a great technique but not a requirement for good software. What do you mean to say in your reply, what is your criticism and why the downvote?
Pekka
OK you say "OOP is no requirement to write good software." But not following an object oriented approach in an OOP language will cause a bad source code. So it is wrong. Is not it? If you edit it, i will glad to upvote. (I cant do it right now as you see)
JCasso
I wouldn't go as far as saying that PHP is an object oriented language only because it now, in its fifth incarnation, finally has serious OOP support. (Don't get me wrong, I love PHP, but that's the reality. :) Anyway, I edited my answer. Feel free to keep the downvote in place if you think the answer is incorrect, though - I just like to know why people downvote. Cheers
Pekka
I got it. You mean "an application written in PHP4 may contain good source code even no OO concept is followed". I agree with you. So with PHP5 and future PHP6, IMHO OO approach will be followed by developers.
JCasso
Yes, it hopefully will, increasingly.
Pekka
A: 

Unless you use an MVC framework with PHP, Such object oriented approach (like ASP .NET has) would take too much time of yours. Actually you will need to design your own framework first.

I cannot say using a true object oriented approach in php is not possible. You can take objects whereever you want and serialize to strore them in session etc...

When it comes to MVC frameworks in PHP, you can see a true object oriented approach.

Check here for Zend Framework example

Zend Framework is not the only one.

But the words "object oriented" are new for PHP. Only PHP5 is considered as having true OOP. So you have to wait 1-2 years more for having good scripts with true object oriented approach.

JCasso
I object to the last sentence of your answer. There are loads of fine, PHP 5 Object oriented products out there. After all, PHP 5 is already 5 and a half years old.
Pekka
So hosting companies provide PHP 5 hostring since 2004 ? No. Php 4 is still something like default, and users are still migrating PHP 5. In 1 or 2 year, I hope PHP 5 will lead. And i did not say there is no good OO products, i said the number will **increase**.
JCasso
Thank god, the majority of hosting companies is gaining speed and moving towards PHP 5. I still maintain that while you are of course correct in your prediction - the number will increase - there is already lots of good PHP 5 software around. I didn't downvote your answer by the way.
Pekka
I am a victim of anonymous php lovers then. I am sorry. I do use PHP5 on my shared hosting for 2 years (personal web site). On the other hand in our company, i cannot provide source code examples in PHP5 for API's we provide. Instead i have to code it in PHP4 because PHP4 still leads. I also wish PHP5 to be new default, but not yet.
JCasso
+2  A: 

i see 3 main reasons:

  • compatibility: PHP did not include OOP from start on. to be down-compatible with older PHP environments and to reach a wider audience, you're keeping the status quo.
  • effort-switch: switching/refactoring a codebase fron non-OOP to OOP is a lot(!!) of work, transformation script in my view aren't a solution, because they tend to generate code for machines and not for humans. this is OK from runtime aspect but horrible for maintenance.
  • team-culture: i talked to some PHP developers, some just don't want to make the switch, because they say everything is good and working...
manuel aldana
I agree with points 2 and 3 but point 1 is only half correct IMO. PHP has had object orientation since Version 4 which is nine years old. It has been possible to work object oriented in PHP since the year 2000, even if it was only PHP 5 that brought the serious OO features.
Pekka
also PHP 4 OOP was slow and so adoption was not as wide as one would expect for an OOP language
namespaceform
+1  A: 

One of the biggest barriers to going all OOP with PHP is that everything has to be reloaded, initialized and executed on every page hit. If you designed a PHP system that was truly object oriented, across the board, performance would probably be abysmal. There would just be a lot of files and objects to load and configure to get things going. Everything has to be loaded and ready to go within a second (ideally much less). Compare that to say a Java based system where it doesn't really matter if it takes 10 minutes to get the system started. Once it's started, everything is loaded and ready to go.

WordPress is probably a good example of how slow things can get when creating a modular system with lots of files. Much less a modular OOP system. Load testing a straight WordPress install will get you about 10-15 pages/sec with a simple "Hello World" page. Compare that to being able to get well over a hundred pages/sec with a straight "Hello World" php script. You can work around these issues with caching, which WordPress, Symfony and other systems do.

Brent Baisley
Hmm ... This caching/loading performance issue, combined with unpredictable availability of caching on shared hosting providers could be a strong reason.
namespaceform