I have a PHP driven site that was started a very long time ago and wish to enhance a particular section.
Current Setup and Functionality
The bookshop sites use AJAX to search for authors, titles, etc. A single home page dynamically displays text results from a form’s input text field. This is working well on all mobile and desktop platforms.
The site was originally PHP Procedural and the enormous library since been converted to three PHP Classes; DataBase, GoogleBooks and StockUpdates. One class has grown to well over a thousand lines and has become remarkably difficult to maintain due to numerous “fire-fighting updates”.
Current work flow
- home page displays
- input search box populated
a. calls AJAX to build a database query
b. database queries relevant table
c. results formatted and rendered to the home page
d. link to Google Books API to fetch book reviews
I would like item #c to display relevant Google Books API book covers. I have achieved this task by adding additional class methods and it has introduced complexities which will no doubt get far worse and require numerous “fire-fighting updates”
I have read quite a few articles on “Refactor vs Rewrite” and favourite is:
JoelOnSoftware - things you should never do
Rather than add to the existing classes I would like to create a simplified stand-alone task to make maintencance easier.
What is the best way to approach this problem bearing in mind I have only a basic knowledge of PHP Classes?
Not a real expert on this, but it sounds like you have classes that do just too much.
It probably needs breakig down to more smaller classes that deal only in specific related tasks.
Three years is an awfully long time in the Internet world.
…Says a 20 year old article.
When the first line mentions Netscape, I had to check the date.
I’ve quite liked refactoring old projects entirely from proceedural to OOP. The biggest problem is finding the time.
Where is can become silly is the fact that, by the time you have completed the refactor, the experience and skills you gain while doing so, makes you think you could do it 10 times better if you started over and refactored again. Save it for your next project.
What has happened is that you have ended up with what is known as “god classes”. As mentioned, they do too much.
You would do well to study and understand the SOLID principles, especially the S which is “Single-Responsibility Principle”. Once you do you can apply those principles to your code should you choose to refactor. You will also benefit by implementing SOLID in any new projects.
This is the challenging part:
Short of seeing the actual source code I don’t see how anyone could really suggest alternatives without knowing how much you actually understand. You could consider posting a link to your source code repository assuming the code is yours to share.
Otherwise, consider just jumping in and creating a Symfony project and re-implementing some of your app. Don’t think of it as refactoring your app so much as a learning experience. You might very well end up tossing the entire project. But you will learn quite a bit about OOP in the process.
I recommend Symfony over some of the alternatives because, from an OOP standpoint, I’d say it is generally recognized as the best in the field and is well supported. But most of the other frameworks would be fine as well.
This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.