Refactoring PHP, Web Services and PHP Frameworks

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

PHPDevelpr
Forum Newbie
Posts: 2
Joined: Tue Sep 28, 2010 5:16 am

Refactoring PHP, Web Services and PHP Frameworks

Post by PHPDevelpr »

Guys,
You might be able to point me in the right direction in terms of design and frameworks in relation to this question.

We have a large PHP application which was written several years ago to provide key functionality to our end users. Over that time frame additional functionality has been added on to the point where the application has become very difficult to maintain and resembles spaghetti. An MVC approach was not used. The application contains key business functionality which we would like to reuse across other applications eg. Java, .Net

It has been decided to refactor the PHP code and add web services to enable other applications to access the underlying busiess functionality.

I would like to get some advice as to the best approach to take.

The existing code is based on no PHP framework and I am wondering how to go about this refactoring. Is it better to choose a PHP framework and refactor the legacy code using a framework like Symfony or Zend or am I better off refactoring the underlying code without using a framework?

Essentially I am looking for the quickest, easiest solution that doesn't consume a huge amount of time to implement.

Any thoughts appreciated.

Thanks.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Refactoring PHP, Web Services and PHP Frameworks

Post by alex.barylski »

I would re-build using Zend framework.

Cheers,
Alex
mikosiko
Forum Regular
Posts: 757
Joined: Wed Jan 13, 2010 7:22 pm

Re: Refactoring PHP, Web Services and PHP Frameworks

Post by mikosiko »

PCSpectra wrote:I would re-build using Zend framework.

Cheers,
Alex
Alex... what about the Zend framework learning curve?... will be that too high for a project like the OP is asking for opinions?... granted the benefits in the long run.

the OP is asking "Essentially I am looking for the quickest, easiest solution that doesn't consume a huge amount of time to implement.", do you think those objectives can be fulfilled even without previous experience with Zend?...

I'm trying to do something similar, therefore I'm interested too in the best approach.
loiclavoie
Forum Newbie
Posts: 2
Joined: Mon Sep 27, 2010 1:14 pm

Re: Refactoring PHP, Web Services and PHP Frameworks

Post by loiclavoie »

Unfortunately, massively refactoring an application is time-consuming. You can't avoid that.
OP, director and project manager will always tell you that they don't have time or money to do it well, it's an universal rule. It's the role of the programmer and is lead to defend the point of that.

Here's some advice from my experience:
#1) Using a framework or a CMS will always give benefit. You will have access to a doc on the internet, tool created by other programmer across the world (time saving on future developpement), access to a community to increase your knowledge.
#2) When using a framework or a CMS, set of coding rule are already defined. So after learning those, you'll have a static way to write code and it'll be easier to track bug and know where to add new feature.
#3) Scaling a application become easier on a public framework or CMS. Why? Because you're not the first one to do it, so you'll be able to find doc on the "how-to-do-it".
#4) Ultimately, even if the cost of the solution will be higher on the developpement, the maintenance will be by far more lower than it would have been by using a homemade solution or a patching solution.

However, even if you demonstrate theses point to your boss, he still want something cheap, fast and now, just patch the actual solution. Do quick-fix. On the long term, document your idea, and your project with proof that major refactoring would be better than keep doing that.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Refactoring PHP, Web Services and PHP Frameworks

Post by josh »

Rewrites are a false economy. If you did so bad the first time round to warrant the rewrite, what makes you think the rewrite will turn out any better on the first try?

Unfortunately the definition of re-factoring, by the man who coined the term, requires that you first have unit tests (otherwise you're not re-factoring, you're "just changing stuff"). For how to get legacy code under test, see this book. The definition of legacy code is anything without a test. Logically, anything you do besides write tests at this point will just add more legacy code to deal with.

One important thing to note is, you don't have to get your whole project under test. But you must follow this rule: no one makes *any* changes to a piece of production code until that piece of production code has "enough" tests. How much is enough? It should have a comfortable amount to where there is no fear of breaking anything.

Its not managements decision, its the programmers. When the manager asks for the quickest way the programmer should be wise enough to forsee the need for the test. Documenting the fact you didn't write the tests may keep you from getting sued but that's not going to make you a more productive or happier programmer.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Refactoring PHP, Web Services and PHP Frameworks

Post by alex.barylski »

Unfortunately the definition of re-factoring, by the man who coined the term
I don't think he actually "coined" the term, he did however popularize it with developers, and give a more clear definition of what it is. I do recall reading of refactoring, and using the term well before Fowlers seminal book: "Refactoring. Improving the Design of Existing Code"

Cheers,
Alex
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Refactoring PHP, Web Services and PHP Frameworks

Post by josh »

I didn't mention Fowler. I was obviously referring to William Opdyke. The first use of the term appeared in his extensive dissertation which outlines all the ideas we practice to this date.
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Refactoring PHP, Web Services and PHP Frameworks

Post by alex.barylski »

Hehe my bad. I mistakingly assumed the link to amazon was for Fowlers re-factoring.

Cheers,
Alex
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Refactoring PHP, Web Services and PHP Frameworks

Post by josh »

Nope, but Fowler is the only source I have to backup the statements I made (that William Opdyke invented it).. just FYI
josh wrote:Rewrites are a false economy. If you did so bad the first time round to warrant the rewrite, what makes you think the rewrite will turn out any better on the first try?
I'd like to correct what I wrote here. I meant what makes you think you'll do any better on the second try.

So what I meant was:
josh wrote:Rewrites are a false economy. If you did so bad the first time round to warrant the rewrite, what makes you think the rewrite will turn out any better than the first time around?
alex.barylski
DevNet Evangelist
Posts: 6267
Joined: Tue Dec 21, 2004 5:00 pm
Location: Winnipeg

Re: Refactoring PHP, Web Services and PHP Frameworks

Post by alex.barylski »

Nope, but Fowler is the only source I have to backup the statements I made (that William Opdyke invented it).. just FYI
Haha...I know your a fan of Fowler...but I was certain it was Booch or Beck that coined the term re-factoring, in either case I would have been wrong.

Cheers,
Alex
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Refactoring PHP, Web Services and PHP Frameworks

Post by Eran »

what makes you think you'll do any better on the second try
* Much better understanding of the domain
* Many edge cases already known
* More development experience and improvement in methodology and tools

I don't see why a rewrite several years later can't be very successful. Whether it's economical or not is a different question.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Refactoring PHP, Web Services and PHP Frameworks

Post by Weirdan »

pytrin wrote:I don't see why a rewrite several years later can't be very successful. Whether it's economical or not is a different question.
It could be worse because of second-system effect
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Refactoring PHP, Web Services and PHP Frameworks

Post by Eran »

We're not talking a new version, it's a rewrite of the same application. Of course, the difference is up to the developers ( /managers / other interested parties)
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Refactoring PHP, Web Services and PHP Frameworks

Post by Christopher »

PHPDevelpr wrote:We have a large PHP application ...

Essentially I am looking for the quickest, easiest solution that doesn't consume a huge amount of time to implement.
I think the OP ruled out a rewrite. ;)

And I have to agree with Josh that rewriting the thing is a bad idea (almost always). First, rewrites and never really rewrites -- it is a whole new application with many of the same features. Human nature never seem to allow an exact copy to happen. I also think that "better" is relative to the level of effort. If it tool 10k man hours to build it and 5k hours to rebuild it. And it is 5% better. Was it worth it?

What I have done (many times :( ) is to first refactor the index.php script to allow a modern/alternate Front Controller (pick your brand) to run in parallel. That simplifies things a lot. Then I build up test support around an area of the app that 1) seems like easy code to refactor and 2) is causing problems (so when fixed there is actual improvement to show for the effort).

Then start cautiously porting pages/sections over to the new architecture. Start with the easy stuff because you need to deal with cross-cutting problems like Access Control where you will need to write glue. Soon most of the porting issues will become apparent and then the process will become more rote. Good test support and quality glue code are a big help. You will need to identify where links come from (grep can help) to keep everything working. And you need a process to track removing old scripts from the system.
(#10850)
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Refactoring PHP, Web Services and PHP Frameworks

Post by Benjamin »

In my experience, the key to successfully refactoring a horrible codebase is to identify duplicate code and utilize decomposition. I've found that a single person can rather quickly take a very large codebase and significantly reduce lines and improve performance by simply locating duplicate code blocks and factoring them into functions/classes. Depending on the codebase of course, this may be easier said than done.
Post Reply