Posts Tagged 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') { ...
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:
Easy Documentation for Git, MySQL, PHP, et cetera
This is what I do on my box to quickly find documentation, which you guys may find helpful. Especially those of you on Linux---although you could do this on Windows too. Most package managers make available '-doc' packages, like php-doc, mysql-doc, and so on. Install these for all the major software you use. Next, install 'screen'. Now put this is your Bash config: # Displays various types of documentation. function doc() { case "$1" in 'llvm') screen -t 'LLVM Documentation' w3m /usr/share/doc/llvm-doc/html/index.html ;; 'erlang') screen -t 'Erlang Documentation' firefox /usr/share/doc/erlang-doc-html/html/doc/index.html ;; 'python') screen -t 'Python Documentation' w3m /usr/share/doc/python3-doc/html/index.html ;; 'php') screen -t 'PHP Documentation' w3m /usr/share/doc/php-doc/html/index.html ;; 'ghc') firefox /usr/share/doc/ghc6-doc/index.html & ;; 'postgresql') screen -t 'PostgreSQL Documentation' w3m /usr/share/doc/postgresql-doc-8.4/html/index.html ;; 'mysql') screen -t 'MySQL Documentation' w3m /usr/share/doc/mysql-doc-5.0/refman-5.0-en.html-chapter/index.html ;; 'apache') screen -t 'Apache Documentation' w3m /usr/share/doc/apache2-doc/manual/index.html ;; 'j') screen -t 'J Documentation' w3m ~/Software/j602/help/index.htm ;; 'lua') screen -t 'Lua Documentation' w3m /usr/share/doc/lua5.1-doc/doc/index.html ;; 'git') ...
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() { ...
