views:

94

answers:

6

i mainly use PHP and I have been coding using it for a while. My main problem with PHP is that I think as your program gets bigger, it gets harder to maintain your code (probably applies for other things). I want to reduce this complexity in maintaining/modifying my codes. I want to make things modular and have rules when to start a new class or just append to an existing class (for example). I know that there are frameworks out there (CakePHP, Symfony, Rails) but what if I just want to use my PHP and use a hybrid of my own style and an existing style for good code management?

+2  A: 

Learn Design Patterns:

Also helpful to increase code quality:

A sidenote on frameworks: while frameworks often do tell you how you should layout your code and folders and stuff, they rarely tell you how to code properly. A framework offers solutions to common problems and that's cool, but if you try to step outside of what the framework already offers in terms of functionality, you are on your own again. That is because frameworks are concrete implementations, while you want to learn about design principles.

Gordon
+1  A: 

This is about code in general (it uses Java/C/.Net for the examples if I remember correctly), but Code Complete's the best book I've read on general code structure. It covers everything, from the nuts & bolts of how to write and organise variables and methods up to program structure. I'm not sure how applicable the examples are to PHP but probably worth a look.

Grant Crofton
A: 

http://www.nettuts.com

everything you'll want to know about PHP and web development.

cdnicoll
that site will tell you how to do proper object design and application architecture?
Gordon
This website is absolute rubbish for any serious programmer. Full of "Top 10 this" and "Functions you didn't know!".
The Pixel Developer
+4  A: 

This is not just pertaining to php, this applies to coding in general. Frameworks will only help you organize your mess but if you are new to programming a framework will only organize your messy code.

Some basic ideas to keep in mind aside from undertaking a undergraduate in computer science (which i recommend if you are serious about coding) is to make your code modular.

For example, and this is a simply example to demonstrate the point. If you have a site that generates an html table containing financial data.

(Here are a few scenarios of how to go about coding this ...)

  1. You open up a new screen, financialdata.php, you code from line 1 to line N the code needed to get the financial data from a data source (database perhaps) and then iterate over the financial data to construct an html table.

  2. You open up a new screen, financialData2.php, you code a function at the top which pulls data from yuor data source and returns an object (array maybe) containing the items. You take this return value and generate your table on this financialData2.php page.

  3. You open up yet another new screen, financialData3.php, you take your database function from financialData2.php and use it to get your financial Data from yoru source. You code another function to generate an html table based on some arguments passed in as parameters. And lastly in your financialData3.php page you display do the following and your page now has a table containing financial data from your data source.

The lesson here: the more modular your code is the better in a lot of ways. You now have a data base function that can get data, and you have a function that will render a table based on a list of items passed in.

You can now create another page and use these functions in different ways, perhaps your data source function has parameters like table and selection criteria. This way you can use the same data function to get data from different tables based on criteria. You now have a VERY basic data abstraction. (Dont let abstraction scare you aware). An abstraction is nothing more then a simplification of something. In this case we have abstracted away the details of how we get data and let our function getData take care of those details.

Please comment/ask questions and we can discuss further but honestly, I do not think one book or web site can teach programming practice like a BS in CSE can through classroom discussion and hands on practice.

Chris
you know.... it's funny how none of my cs classes taught me how to code "in a clean way". One of my professors told me that students coming from my school are known to be bad bad messy coders. I consider myself as one of them now.But school project is unlike real job project. With real project, you are never done with it. Class project, you are done (most of the time) once it's graded.
denniss
I agree, formal education is more about learning how to learn and teach yourself. specifically, how to attack a problem and solve it. (Not to say said solution will not go through countless revisions.) Frankly, my BS taught me how to approach a problem, abstract it and formulate an algorithm to solve it. We also at my institution, focus on "design/program by contract" methodologies. This along with years of hands on has helped my code mature.
Chris
A: 

http://www.amazon.ca/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612

Lots of design patterns explained. But not in PHP. Good for understanding useful design patterns. Support the author by purchasing it.

KrNel
If it is good and useful, why not pay for it then?
Gordon