views:

496

answers:

3

Hi, is there any easy way to convert Procedural PHP to OOP PHP. I've a web application, and currently, only the login system is in OOP.

Thanks :)

+4  A: 

Simple answer? No. OO is a different programming paradigm. You can't divine an object model from procedural code. With many PHP inbuilt functions there are object equivalents but it's not an automatic conversion type of thing.

But let me ask you this: why do you want to convert it to OO?

I ask this because to me there seems to be a kneejerk reaction from many developers to simply OO-ify things whether it makes sense or not and whether it improves readability, maintainability and functionality or not.

The beauty of PHP to me is that it's simple. When used for Web development (which is 99% of PHP's usage) PHP has a request-based lifecycle. Each script execution represents one HTTP request. That model is very procedural by nature. Sure use an object model if it makes sense and makes your code cleaner. Use it in parts if you want to. But serving an HTTP request is often a very procedural thing to do.

cletus
well, although I'm the only developer currently, we'll be getting more, and the code needs to remain maintainable. It's also current standard practise, from why I have gathered to use OOP as opposed to procedural.
Shamil
*"It's also current standard practise, from why I have gathered to use OOP as opposed to procedural."* If that's the case, I don't want to offend you but, you probably don't know enough OOP to write good OOP.
Ionuț G. Stan
+6  A: 

I would say "no" :

  • you would have to "convert" everything, probably by hand
  • which means encapsulating all your functions in classes, and the like
  • and modifying every place where you called those functions.

After that, you'll have classes and methods, but your code will still probably not really be "OOP" : OOP is more than just having classes/methods instead of functions !


As a sidenote : if your website is working, why do you want to "convert" it ?

If it's to facilitate maintenance or evolutions : I don't think what you need is a "conversion", but more like a "rewrite", if you want real OOP.

And OOP is not necessary if you want a big application, with many developpers, that can be expanded ; for instance, take a look at the CMS Drupal : it's developped with procedural code (and some classes here and there), but many people work on it and/or develop modules ; so, it is possible -- having strong conventions, of course, is necessary.


Stuff like MVC, Design Patterns, whatever buzzword you can hear, are great... But you must think about them when you are doing the conception phase of your application -- when the application is finished, it's a bit too late...

Pascal MARTIN
rewrite - damn :/
Shamil
Also, it is by far, not completed.
Shamil
If it's far from completed, maybe stopping developping for a couple of days, to have time to **think** about what can be done, would be a good idea (it's a bit late for that, but better now than in 6 months, I guess) ;; hopefully, maybe yu can come up with a way of getting things better without too much work ? ;; still, what I said remains true, of course.
Pascal MARTIN
Thanks Pascal :)
Shamil
You're welcome :-) ;; one last thing : when you start having a team of other developpers with you, explain to them what the conventions on the project are, what that can do, what they cannot, how they can do something : it's generally a good way to help every one not do too much stuff the "wrong" way ;-)
Pascal MARTIN
A: 

Damn, I was hoping there is an easy way, but nope and it is too late for my company's internal web application. It is already finished long time ago, and there is about a hundred + PHP pages scattered and disorganized structure and very hard to maintain. All this problem will not occur in the first place if the first guy who coded this had took time to do some UML and code it in OOP!