Organizing your PHP code?
I’m a PHP developer and in the attempt to better myself in my coding practices I’m looking for a different method of organizing my class methods, functions files etc.. Right now I’m kind of randomly defining functions without much organization beyond using classes to separate things out.
Is there a good standard to follow?
Observing members:
0
Composing members:
0
3 Answers
Even though I largely work with symfony and CakePHP, I tend to follow the Zend Framework coding practices. Generally, any private and protected methods and members are first. I usually list them in the order they’re used, but that’s not a requirement in PHP. In my classes, the __construct function is the first public function, followed by the other public functions. While Zend may not do this (I’m not sure, actually) I put static public methods at the very end. I couldn’t tell you how to organize data members beyond there since I virtually never, if at all, use any public members.
If you’re looking for clean code, use self-documenting names. Don’t be afraid to use longer names if they’re more descriptive. I would rather a method be named getUserInformationFromDatabase than it be called something like userInformation, etc. If you don’t already, use phpDocumentor comments for all of your files, classes, data members, and methods.
+1 for following ZF. I really started to write neat code once I got used to that.
Oh, and mostly you’ll want the name of your functions/methods to at least start with a verb (as they should “do” things). That would help with @noyesa‘s second point, i.e. “getUserInformation()” as opposed to “userInformation()” (you’ll generally not want to mention the source (the database, in @noyesa‘s example) as it’s the output you’re interested in, not the way it is retrieved).
Agreed, the source should be self-evident, or at most written in the docblock.
Answer this question
This question is in the General Section. Responses must be helpful and on-topic.