views:

135

answers:

6

hey there

Maybe this isn't the right place to ask, but I was wondering if there is any advice on how to start fixing an old-fashioned-style php script.

A few days ago I received an offer for developing an old PHP project, and by old-fashioned I mean the structure did not use OOP coding method and it doesn't have a definite framework.

I am confused on where to start, and wanted to know , what methods there are for developing an old script.

Note: They don't want to spend lots of money on starting a new project.

So what methods would you suggest for updating an old php script?

+2  A: 

It depends what you mean by "old". Old as in written for PHP 4? Or old as in non-OOP? (Or both?)

Old as in PHP4:
As long as you sift through it and either suppress warnings or actually fix deprecated function calls everything should be fine. This is just simply boring work. Easy and cheap.

Old as in non-OOP:
One could theoretically develop a very stable and scalable app without OOP or a definite MVC (or other) framework. As a matter of fact, if the app is small in scale, there's no reason to add the spaghetti and meatball complexity of OOP or a framework. Re-writing everything in OOP with some framework is hard and expensive. And quite probably overkill.

David Titarenco
It says non-OOP in the question.
Wayne Koorts
+1  A: 

Can you give us more detail, perhaps an example.

Even procedural code has elements of OOP in it. You can identify variables and procedures that relate to the same entity. You could go about rewriting it, but they're going to have a hard time finding value in it, especially if they are frugal, as you suggested.

cinqoTimo
A: 

Perhaps Your code right now looks like this alt text

And you want that it looks like this

alt text

Well if its just a script and not the whole Project i would convert it to OOP coding standard.

streetparade
Does PHP even have a `goto` keyword?
Earlz
+2  A: 

Joel Spolsky writes:

"[Netscape made] the single worst strategic mistake that any software company can make: They decided to rewrite the code from scratch."

http://www.joelonsoftware.com/articles/fog0000000069.html

So, whatever your course of action, priority is to work with the existing code. Refactoring will be one of the best methods you can use.

What can you not do, if the code base is not updated, that you absolutely must? How much and of what particularly do you need to upgrade for that action to be possible? Consider these two questions.

erisco
+1  A: 

When I do this, it's a multi step process. Typically, there's an existing product to keep running. Rewriting from scratch is rarely an option, even though you end doing it eventually.

  • Begin to ditch manual include statements and implement an autoloader, where possible (takes many passes)
  • Create a helper script to simulate magic quotes & register globals. This is so you can turn it off in PHP, while keeping the existing code running
  • Gradually remove excessive strip_slashes or add_slashes calls, if applicable. The helper script allows you to do this per file.
  • Ensure that your variables have proper scoping
  • Separate out your presentation code. Consider Smarty or alternate template system
  • Move the DB calls to PDO and use parameter substitution for everything
  • Look at the code and think about stubbing out a front controller

I then look at the project and determine how I'm going to alter the logic itself. Often, if there are no functions at all, my first pass is to wrap common behaviors into static methods. Get as much reuse without too much effort, so I'm not concerned with organization yet.

After the redundancy is reduced, then I get to organization. It's at this phase that I start planning out my class models and refactoring the functions into clean methods. This is also the time for automated tests (phpunit). Once I'm reasonably confident, I add some controllers and integrate the templates, then I'm done... barring one or two more passes.

For me, it's all about identifying where I am, where I want to be, and making a plan that can be executed in several small steps. Everybody has their own objectives, so there's no magic plan to follow except your own.

Pestilence
i like this answer , its something :D tell me more about PDO
farshad Ghazanfari
PDO is an OOP database library for PHP. It offers iterators, prepared statements and a consistent interface. http://php.net/PDO
Pestilence
A: 

Read their code. Talk to them.

Look at the requested change in terms of the existing code. Talk to them.

Decide how little of it you change to do what what want. Talk to them.

Do that. Talk to them.

When they ask for functionality that can be more easily done by re-writing than by modifying, do that.

Work with an IDE that can assist with refactoring.

Don
so u r suggesting me to talk to those stupid business men , my main goal is to secure this project and develop it in a way that i can stabilize the project
farshad Ghazanfari
You most assuredly can not do that without their support. And you mentioned something about "Note: They don't want to spend lots of money on starting a new project." This kind of activity smacks of project management and not programming. BTW: they can't possibly be "stupid businessmen" if they have money to spend on programming and know enough not to spend too much! Be careful of being seen as a "stupid programmer" who can't make a simple change.
Don