开发者

How to fix an old coding style php script

Is there 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 开发者_JAVA百科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?


Joel Spolsky writes:

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

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.


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.


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.


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.


Perhaps Your code right now looks like this

How to fix an old coding style php script

And you want that it looks like this

How to fix an old coding style php script

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜