|
|||||||
| PHP Discussion and technical support for using and deploying PHP based websites. |
![]() |
|
|
Thread Tools | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
PHP style critique
I think my code is horrible. Granted it works, but everything is a jumbled mess, and cluttered, everything shoved into one page. I was wondering if I could get some pointers on some ways to clean my code up.
Here's an example of one of my pages: PHP Code:
My input forms are even more horrid and I can include one of those too if anyone is willing to help me pick up some better coding styles. Any help will be greatly appreciated! |
|
#2
|
|||
|
|||
|
Trust me, I've seen way worse... One of my co-workers doesn't quite get the idea of Classes so he ends up making 3-4 different ones for the pages he's making and uses them sort of like a spaghetti\procedural hybrid.
As for this it's more of a personal choice. Unless you work somewhere that has unified code (I will finally have that). Personally I go with more of a template style of coding. Looking at this page I notice you have the dynamic header and footer going on. I go the other way, each inside page is dynamically added to a main template. Looking at your code you are consistent which is another major thing. Noticed you use the if else without brackets but again you're consistent. As for me (coming from perl to PHP) everything that it's optional to have brackets has brackets. Again, that's personal, so my code looks like: Code:
if($something==true) { print 'cool'; }
if($bacon=='is tasty') {
print 'You know it!';
exit;
}
Two last tidbits. Here all classes are first letter capital $Class = new Class(); and all variables\arrays are first case lower $some_variable (yeah a fan of the underscore). The other tidbit, use a colored\formatted text editor like Notepad++ if you don't already do. That's my weapon at home, work it's Dreamweaver (Code view only) due to the setups. Wow, longer than I thought that'd be...
__________________
Mullanaphy! http://www.mullanaphy.com/ Unless code is provided or an exact example is requested I think I'm going to start using psuedo code from now on... Also, I freelance as well. Inquire within! |
|
#3
|
||||
|
||||
|
A couple of things which will have a big impact. Firstly try to get away from procedural programming for all but the smallest scripts (and some would argue even for those) and learn object oriented programming. It takes a while to get to grips with, but once you get it you will wonder how you ever coded any other way. Another tip is to separate your logic from your presentation - seeing lots of html mised in with PHP all the way through a script is never a good sign. It will make your life easier when it comes to debugging too. I personally tend to go one step further than that and use the MVC paradigm in larger projects in order to separate data management, business logic and presentation, but i'd worry about that after learning OOP.
|
|
#4
|
|||
|
|||
|
Shorts: Could you elaborate on how you work everything through a template? Thanks for all your other suggestions. As for my editor, I use Dreamweaver basically just because of the ability of uploading files automatically when I save them. I would gladly try any other editor though because Dreamweaver is a resource hog and all I use it for is a text editor and a ftp solution. Any suggestions?
Mindzai: Well I understand OOP, I can even create classes. I have a couple small classes that I utilize for various functions. I know it would help me in situations like this because I have several pages like this. My biggest issue with OOP, and I've been struggling with this for a while, is how to apply it in situations like this. What type of class(es) would I make here just for simple presentation of data? I guess I just have trouble splitting my data into classes where it would be simpler than some procedural code. Can you suggest any practices or give me some examples that would help me out here? As for mixing logic and presentation, just do all my logic at the top of the file basically? And then when I want to display info, just use something like "<?=$some_info?>"? That wouldn't be hard to fix, and would make it look cleaner, and I've actually thought about doing that a couple times. Thank you both for you suggestions! |
|
#5
|
||||
|
||||
|
When I see this...
PHP Code:
PS: As far as editors, I've been using NetBeans 6.5 lately and find it to be a pretty good combination of features, performance, and price (free). See this article for one thing that I did not find at all intuitive about it, though.
__________________
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ Terry Pratchett in Nation Kindle Minds (blog about Amazon Kindle) |
|
#6
|
|||
|
|||
|
NogDog: So basically have a class thats comprised of queries? So basically each function is something like:
PHP Code:
Last edited by bejitto101; 04-22-2009 at 05:49 PM. |
|
#7
|
||||
|
||||
|
The idea is to think in "objects". For instance, looking at that query in the preceding post, presumably you have some "thing" you are modeling in your application called a "business". So the OODesign approach would be to create a Business object. This object would store the data (class variables) for a given business as well as the things you might do with that data (class methods). One of those methods would likely be for populating the Business object data from the database. Its argument would be the ID number, which would then be used in a query to get the data:
PHP Code:
PHP Code:
To take it a step further, you could have an abstract class that contains all the basic functionality, and then have each specific class (such as Business) extend that abstract class, only then needing to add code specific for that object type (such as the keys for the $data array).
__________________
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ Terry Pratchett in Nation Kindle Minds (blog about Amazon Kindle) |
|
#8
|
|||
|
|||
|
thanks for sharing
|
|
#9
|
|||
|
|||
|
Thanks NogDog for the advice.
Unfortunately, I don't believe you can use prepared statements to get a result set with mysqli, a big drawback in my opinion. I really like prepared statements, it makes things a little simpler. The only thing you can do with a prepared statements is bind the parameters. I guess I would just have to bind each variable to $data individually, or is there a better way to do this? |
|
#10
|
||||
|
||||
|
Quote:
PHP Code:
__________________
"Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ Terry Pratchett in Nation Kindle Minds (blog about Amazon Kindle) |
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|