Monday, June 23, 2008

Time To Revisit PHP.Net



We don't know it all, even when we feel like we do.

Sometimes we get stuck in our ways and think we know it all. It's interesting how I am taught a thing or two by others. Anyway, if you haven't been over to check out the latest function list with PHP.Net, I suggest you do so. For instance, I used to do posted form data entry validation and filtering/cleanup with my own string functions and preg_ (pcre API) because I had my mind stuck back in the days of PHP4. Well, turns out that everyone's now using the new filter_ API, and there's a ctype_ API out there that I didn't even know existed.

Which do you think runs faster? A bunch of calls with preg_ API or one single call to filter_ or ctype_ API? Well, each time you use a function in the global namespace of PHP, that's a slight performance hit. Of course, that hit is faster than your own code with loops and if/then's because it's calling something in C, but at least consider that one C call is going to be faster than three or four C calls. So yeah, using the filter or ctype API is going to speed you up a slight bit.

Another one to look at is mcrypt API, if you were doing your own encryption by hand. However, you can still keep your old makeshift encryption functions on hand -- just do an extension_exists('mcrypt')) call to see which API can be used on a given server. Oh, and if you've never used extension_exists(), then neither have I until today. Again, go look over the PHP.Net stuff because there's lots of new stuff in there with PHP 5.2 or better.

Of course, your server may not have these features, but they should! You can either get your web hosting provider to upgrade to PHP 5.2 (or higher) or you can see if you can get them to add the filter API extension (even if beta).

Next, the more I use an ORM, the more I appreciate it. I should have used Outlet ORM (or any ORM, such as Propel) awhile back because I would have accomplished a lot more in very little time. And no, you don't have your hands tied with the ORM -- if you are working on a complex JOIN and don't want to fight with the ORM, then don't! Instead, go grab PDO or mysql API and bring it on, old-school.

Another piece of advice I received was to use APC to speed up the performance of my pages. You can get this from PECL with 'pecl install apc' at command line.

Some more advice I received was to use page recursion on form posting. I used to HATE this it. HATE, HATE, HATE, HATE!!! It was because it would confuse the heck out of me when inheriting code like this. But now I see the light. You see, it's far easier to do data validation and kick the user back to the same form with the data filled in and an error message than it is to redirect to another page with duplicate content, or show an error. So, yeah, my advice is to do page recursion on at least most of your form posting, but do it tactfully, keeping in mind that you will have programmers one day who inherit your code. So make good use of comments, and make the if/then logic clear such as:

if ($bFormPosted) {

or at least:

if ($_POST) {

Well, gotta run. Hope this helps!

No comments: