How to fix old coding style php script

Are there any tips on how to start fixing php script in old fashioned style?

A few days ago I received a proposal to develop an old PHP project, and in the old fashion I mean that the structure did not use the OOP encoding method and does not have a specific structure.

I was confused where to start, and wanted to know what methods exist for developing an old script.

Note. They do not want to spend a lot of money on launching a new project.

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

+6
oop coding-style php
source share
6 answers

Joel Spolsky writes:

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

So, regardless of your course of action, working with existing code is a priority. Refactoring will be one of the best methods you can use.

What can you not do if the code base is not updated, what should you absolutely do? How much and what do you need to update this action? Consider these two questions.

+3
source share

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

Old as in PHP4:
As long as you sift it and either suppress warnings, or actually fix obsolete features, everything should be fine. This is just boring work. Easy and cheap.

Old as in non-OOP:
Theoretically, you can develop a very stable and scalable application without OOP or a specific MVC (or other) structure. In fact, if the application is small in scale, there is no reason to add the complexity of spaghetti and OOP meatballs or frames. Rewriting everything in OOP with some perspectives is difficult and expensive. And, quite likely, bust.

+2
source share

Can you give us more detailed information, perhaps an example.

Even the procedural code contains OOP elements. You can identify variables and procedures related to the same object. You can rewrite it, but it will be difficult for them to find value in it, especially if they are economical, as you expected.

+1
source share

When I do this, this is a multi-step process. Typically, an existing product exists to continue working. Rewriting from scratch is rarely an option, even if you end up doing it in the end.

  • Start manual startup instructions and implement the autoloader where possible (takes many passes)
  • Create a helper script to simulate magic quotes and register global variables. This means that you can disable it in PHP while maintaining existing code.
  • Gradually remove excessive strip_slashes or add_slashes calls, if applicable. The helper script allows you to do this for each file.
  • Make sure your variables have the correct scope.
  • Separate your presentation code. Consider Smarty or an alternative template system.
  • Move database calls to PDO and use parameter replacement for all
  • Look at the code and think about lowering the front controller.

Then I look at the project and determine how I will change the logic itself. Often, if there are no functions at all, my first step is to wrap the general behavior in static methods. Get so much reuse without much effort, so I'm not organizing yet.

After reducing redundancy, I get an organization. At this point, I begin to plan my class models and refactor functions into pure methods. This is also the time for automated tests (phpunit). As soon as I'm confident, I add some controllers and integrate the templates, and then I finish ... with the exception of one or two passes.

For me, all this concerns determining where I am, where I want to be, and creating a plan that can be completed in a few small steps. Everyone has their own goals, so there is no magic plan other than your own.

+1
source share

Perhaps your code now looks like this alt text

And you want it to look like

alt text

Well, if its just a script, not the whole i project, it will convert it to the OOP encoding standard.

0
source share

Read their code. Talk to them.

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

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

Do it. Talk to them.

When they request functionality that is easier to accomplish by rewriting than by changing, do it.

Work with an IDE that can help with refactoring.

0
source share

All Articles