开发者

small php project is now a huge mess (inside)

I started a small project for me and a few friends to edit a few tables in a multi-database (mysql). Now the project is over several hundred pages and while it looks incredible on the ouside, it is stating to feel cluttered inside. no structure. here is what we have:

3 databases several hundred tables make up the three DB. The php project is designed to make it easy to edit these tables instead of manually.

does anyone have a suggestion how to organize the code. I a starting to see repeated includes at the top of files, certain code is starting to repeat (I have functions for the more common ones) I would like to stay away from "CLASS" type programming (unless you feel this might be best) only because it is an open source project and some of my friends are not that great at php, so want to keep it simple. but for organization, I could go to class style.

my biggest concerns is that the majority of pages (the html part) are tons of cut and paste. so each page is like the other. not sure how 开发者_开发问答to consolidate those efficiently. I think once that part is figured out, the php code will trim up as well.

thanks


I'd go for the Object Oriented style here (or class type programming as you said :)). This will cut your code massively, it will also help if you need to change a function which is on multiple pages rather than changing multiple functions.

Your friends will thank you in the long run, especially when they embrace the goodness of OO.


If you mean "CLASS" as in OOP (Object Oriented Programming) it's definitely something you should consider. Arranging methods in objects is very convenient once you get used to it, when you have discovered the autoloader you'll know why.

You should also take a look on the market of MVC frameworks. MVC stands for Model, View and Controller and is a fairly common pattern amongst applications. I'd recommend looking at CodeIgniter which is very easy to get started with, even without an extensive PHP career.

If you by any chance would stick to the 100% interpreted, in other words: spaghetti and functions. I'd split everything in to files grouped by their area of functionality. Like: media.php, database.php et.c. Take a look at WordPress and the wp-includes folder and see how they've solved it. Good luck!


There are a lot of things you can do differently here. Here are three to get you started:

  • You're going to have to move to object-oriented programming, especially if you're wanting to go the route of code organization. Keep with the DRY principle at all times.

  • With that in mind, check out a good frameowrk. I would recommend CodeIgniter. The MVC design pattern will remove a lot of the redundancy in your code if you use it correctly. If you choose to not go down the framework route, I would definitely look at some templating libraries to help you out.

  • Normalize your database. This will help you remove redundant model code.


My suggestion is to use a XML-like abstraction layer for all the database fields and also use OOP where you can and understand. You can use XML-File-configuration to separate the database-logic and you can use the php XML-extension to parse it. Now if you have your XML-tree in the memory you can parse it again with an engine to output the html stuff. I use this a lot for example TYPO3 uses this too, but not a XML but a very large array. And also using this you can create your own XML-language and attribute. It's a bit like XLST but not as deep but it's better then TYPO3.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜