Q&A Session for Seven Steps To Better PHP Code - Part 2 Session number: 572043251 Date: January 23, 2008 Starting time: 08:43 am _________________________________________________________________ William Jaspers - 9:12 am Q: shouldn't this be a switch() ? _________________________________________________________________ You could definitely use a switch as well. I would use a switch if there were more commands to choose from, but put the code for each command into a separate function then. This would also allow for selectively loading code, but for very small functions of just a few lines, this probably has no real benefit. John Bertucci - 9:15 am Q: For "get_input($aName,$aValue)" example. Isn't that adding a lot of overhead, particularly for PHP to call a function 3 times and output result, instead of just outputing the value? _________________________________________________________________ Of course calling a function is always a bit slower that putting in hardcoded HTML. As you'll see later, functions open up new possibilities, and PHP code is actually very, very fast when no database access or heavy I/O is involved. So, yes, there is some overhead, but it'll probably be just a aplit millisecond. derby x - 9:20 am Q: what's your method to organize these helper functions? One large file? or each in a separate file? ________________________________________________________________ I like to start off with putting them into one file, then splitting up that file when it gets too large. Splitting up into files basically only makes sense when you selectively load them. For helper and library functions, it's always a bit difficult to load selectively since you never now which function you are going to need in your code. So from the performance perspective at runtime, loading one larger file is probably better than loading several small files (due to the latency of disk access). It depends, however, on your system and application. The best idea is to actually benchmark which way is faster for your. And consider using a bytecode cache, which will speed up things. Bill Bolte - 9:52 am Q: is there a reason you compare the value to the variable as opposed to comparing the variable to the value? i've never seen it written in the way you show before. referring to : 'add' == $action as opposed to $action == 'add'. _________________________________________________________________ Yes. When I forget one =, then PHP assigns a value. Since it's not possible to assign to a string, I get an error message, whereas when I write $action = 'add' I have a rather hard-to-find error in my code. Patick Cummins - 10:03 am Q: What do you think of the Symfony framework? They have already provided a framework for doing all of the things you have mentioned in this presentation. _________________________________________________________________ I love Symfony, though I personally don't use it too much. I don't like being locked in to a certain way of doing things, but that's just my personal view. Symfony is a good starting point to create a PHP-based web application without a lot of effort. It might be worth taking a look at. The disadvantage, however, is that it takes quite a while until you familiarize yourself with Symfony. In plain PHP, you quickly see results, with a framework like Symfony there is a learning curve. Amir Jebelli - 10:09 am Q: :i mean is there any way for thread base programing in php ? _________________________________________________________________ Currently, there is not. I don't think that threaded programs would actually fit into the PHP model, since PHP by design uses one process to serve on request. Apache and thus also PHP itself can run multithreaded when the operating system supports it. Multithreading saves resources, but also adds problems with certain PHP extensions like GD. At the PHP level itself, I have never seen a valid use case for threading.