PHP
WordPress – Sharing A Base Class Amongst Plugins
Introduction The new series of MoneyPress plugins that is coming out in the next month is going to be based on a common foundation. This allows us to maintain consistency, share new features across the product line, and provide an improved quality product that gets out to the consumer. However, during the migration to this new shared platform we uncovered some problem areas deep within the bowels of WordPress. Yes, even with the recently released 3.0 version. However we don't blame this on WordPress. Far from it. WordPress is a well engineered application, it's only fault is being tied into archaic versions of PHP... which means anything prior to PHP 5.3 when namespaces were finally introduced. There is a reason many languages have had namespaces for years, but that is a discussion for another post. One of the more nagging problems was ...
More PHP Woes
I’ve been screwing around with the error response codes for CafePress and finally reached a dead end. My only choice if I want to support 4.3 (as the required version for WordPress 2.9.2) is to use this wonderful gem that Eric & Chris shared with me last night: $result = @file_get_contents($url); That hides all warnings & returns false into the $result variable. Why does file-get_contents() return an error? Because CafePress, rightfully so, returns a header with a 400 code saying “bad request”. The fact of the matter is my request is bad, I’ve input an invalid section ID, just as a client might to by accident. Rather than barf all over their web pages I’d prefer to catch the error gracefully, tell them about it, and move on. The problem is that file_get_contents will NOT fetch the content of ...
HTTP Errors When Uploading/Connecting in WordPress
Having problems browsing themes, uploading plug-ins, or doing just about anything that "talks" to the outside world via Wordpress? We have had a development server buried deep in our network behind several routers and firewalls that had a similar problem. Whenever we'd log into the dashboard we'd get various timeout error messages on each of the news sections. We'd not get our automatic update messages whenever there was a plugin update or a Wordpress update (3.0 is coming soon!). Well it turns out that we needed to fix 2 things to help speed up the network connection. Fix #1 - DNS Resolution We run this particular development box on Linux. That meant updating our /etc/resolv.conf file to talk directly to the DNS servers. If you use DHCP configuration or go through a router this file is often ...
array_key_exists() versus isset()
Everyone here has the good habit of testing for the existence of keys in a hash before accessing them, since otherwise logs can quickly fill up with notices. There are two ways to make this check: 1. array_key_exists() 2. isset() You should prefer to use isset(), as it is significantly faster. Here is a test and the results I got on my computer: for ($i = 0; $i < 1000000; $i++) { if (array_key_exists('not_here', $foo) && $foo['not_here'] === 'nomnom') { echo "Not here\n"; } } This took 20.5 seconds to run. for ($i = 0; $i < 1000000; $i++) { if (isset($foo['not_here']) && $foo['not_here'] === 'nomnom') { ...
Consumer Web Apps with Abundatrade
When Abundatrade decided to take their project to the next level they chose Cyber Sprocket Labs to help them get there. They brought their existing website to us and asked us to help. They were looking for a more fluid, more enjoyable web experience for their users. They needed an updated easy-to-use web calculator & they needed it fast. Cyber Sprocket did the job quickly & did it right. More than a year after that first quick "help us fix our site" project we are still helping them push their technology further. We are now not only helping them slowly morph their consumer services into something bigger & better, we are also helping them run their operations more efficiently by integrating the web technologies with their daily operations & services that run the business. The main part of the consumer interface is a product valuation calculator. Using ...
Messing With Namespaces and Anonymous Functions
PHP 5.3 has been available for a while now. But since it will likely be a while before I see it on most servers, I didn’t rush to get 5.3 and mess around with it. It’s not like I’ll be using the new features any time soon. But I’m using the beta version of Ubuntu 10.04 and it comes with PHP 5.3. So since I have it now, I figured it was a good time to familarize myself with the new changes. Humorously, I forgot to update my local copy of the PHP documentation. So I thought about how the two main new things I wanted to try out were namespaces and anonymous functions, and then put together a test script by taking guesses at the syntax, based on error output from PHP. Here’s what I came up with:
Real-Time PHP Error Messages in Emacs
I was frustrated yesterday while working on a project because of a bug caused by a syntax error. In the year 2010 we really should not be having bugs because of syntax. I say this not to blame anyone—in fact the syntax mistake was mine—but to make the point that this is not the type of bug that should ever waste our time. Flymake is a package for Emacs (I think it’s standard these days) which performs automatic syntax checking. Like most things in Emacs, it is extensively configurable. Flymake has to be this way by design, since it is intended to perform real-time syntax checking for all languages. One way Flymake does this is by deferring to external tools. PHP happens to have a syntax checker built-in. If you run php -l foo.php you will be told whether or not foo.php has any syntax errors, and ...
Custom Site & Store Builder with Energy Inc.
The Energy Detective (TED) is a consumer based product that helps home users track their energy usage on a per-device or cross-household level. When Energy Inc, the makers or TED needed to upgrade their site with an easy-to update content management system (CMS) and the addition of a custom storefront, they came to Cyber Sprocket Labs. Within months we had ported their old static-page driven site to our new custom site builder. They could now easily update their own content without getting developers involved, and better yet - the system protected them from inadvertently breaking their site design. The staff at Energy Inc. soon became experts at the system and added new content as well as new product models to the site. The site also started with a simple storefront module. It allowed Energy Inc. to upload new products and track inventory levels to ensure ...
PHP Puts the Un in Unset
Recently I have seen---both in our code and that of others---the use of unset() in PHP as a means of reclaiming memory. I do not think this a good practice, and in my opinion we should not consider it part of our toolbox. Mainly because I worry it gives a false sense of aid, when in reality unset() rarely has any impact. PHP is not a language that gives you fine-grained control over memory. You do not have control over when variables are freed from memory, and you cannot force PHP to release memory. You can give hints to PHP, and unset() is such a hint, but that is it. If you are relying on unset() to help your memory problems, it is indictative that you already have a larger problem, which unset() is not going to solve. I have seen unset() used in these ways most often: function foo() { ...

