Ask HN: PHP Tricks and Best Practices?
I have scoured the YC forum for a post on great PHP tricks and best practices, but found nothing. Maybe I'm not looking hard enough, but I would appreciate it if anyone knows of great sites or tutorials. I use CodeIgniter (excellent classes, typical MVC setup) and jQuery (uses a lot of method chaining, huge fan of this). But I still find myself writing a lot of native PHP for my app.
For example, I wasn't aware of the list() function until someone was helping me write a method to convert times. I've noticed when working with another programmer on a project that he's using sprintf() instead, and it surprisingly makes the code a lot cleaner. I'm finding myself doing more algorithm-style methods, dealing a lot with arrays and a bunch of if's/conditionals (tertiary if's are great!).
Just using these as examples, I feel like there should be a cheatsheet of best practices and ways to minimize the amount of native code written somewhere but I'm not seeing it =]
*Edit: I'm going to open up CI's class files and look at how they wrote everything. Perhaps a good way of doing this is to look at other frameworks? Any suggestions there?
37 comments
[ 5.4 ms ] story [ 95.5 ms ] threadFor example, a profile link:
Write a simple function: And use it: I used to think the extra code upfront wasn't worth it, but after dealing with a bunch of 300+ line templates for a while, I can tell you that it definitely is.Also, you can see a use of string interpolation (a common use case for sprintf), ie "count is: $count"
I've always hated that in smarty, associative arrays are accessed with a dot. Associative array indices don't have to be valid php variable names, so what happens when you want to access $user['home-address'] in smarty?
Smarty 3.x has a legitimate lexer/parser so the templates will be much more flexible. I think they finally realized that since they are compiling the templates anyway the parsing can be slow.
http://smarty.net
Even as is though, it is worth using.
Maybe a simple website with little boilerplate functions would be something to do as a weekend project?
At any rate, a downside to this approach is that you can't easily add a class or target (or any other attribute) to the A tag (at least in your simplified example).
Of course you could have an optional named parameter that would allow you to pass them in, e.g.:
But trying to do method-chainey sorts of things in PHP quickly runs into the language's clunky syntax, not to mention keeping track of exactly what level of quotation you're at, and so on. This is one of the reasons I like Ruby, as this kind of stuff is much more natural. Something like this: Both of these are probably a little denser than a sane person would put into production code, but the Ruby example is much more human-parseable to my eyes. (I'm using rails helper 'h', which could easily be defined in PHP as well).And yeah, in real code the helpers sometimes need to get a bit more advanced. Something I like to do is add a class name corresponding to the function name (which I usually grab from __FUNCTION__) -- it helps keep a nice consistency between server-side/html/js/css
Other than that my personal preference is to use jquery for form submits and manipulation, smarty for templating and everything is tied together following MVC ideals. Class inheritance is where it's at too. Simple one is that your Page class inherits the functions in your DB access class which inherits your config class. Silly I know but it works great for me. Which in the end is the point.
Such as:
http://us.php.net/spl
http://www.php.net/~helly/php/ext/spl/
The documentation on php.net is pretty lacking. Heres are some tutorials and examples:
http://www.phpro.org/tutorials/Introduction-to-SPL.html
http://devzone.zend.com/article/2565-The-Standard-PHP-Librar...
Overall though, I find out about a lot of cool things just from the comments on the php.net site. For example, I learned how to use pcntl_fork from the socket_accept page, and then I found out about socket_create_listen from one of the process control pages.
My approach is basically just to figure out what I want to do on a higher level, and look for sample code on all the individual parts. Then who knows, I might discover something amazing.
It has a lot of a lot, so you don't have to write so much native PHP. Even Rasmus recommends it as his "framework" of choice (although he does not "like" frameworks).
With regard to suggestions to check out Zend Framework, my advice is: Don't.
http://us.php.net/manual/en/language.types.string.php#langua...
You should recognize this immediately if you have done any unix shell scripting.
I use it to spit out large pieces of HTML.
function user_profile_link ($user) { echo '<a href="/user/', $user->profile, '">', $user->name, '</a>'; }
MediaWiki has in my experience a very well-written codebase, so I would designate it a good candidate for code-reading.