First, some PHP stuff.

The e modifier of the preg_replace() function escapes things. It can be round tripped, but it seems silly that you have to do so manually.

It's possible to call a method using preg_regex_callback(), which does not escape things, but you have to pass it array('objectname','methodname').

And, other stuff.

I like Blonde Redhead, Quite Village, and Santogold. Thanks to @turtlepark for the recommendation, and Chris Dahl for seconding.

Lastly, I get grumpy when I haven't had enough sleep.

I've been working on Habari a bit recently, so have been writing more PHP than Ruby lately. Given that Habari's code quality is pretty good, that hasn't been too terrible. However, I recently had to prepend a URL to a bunch of file paths, and was thinking in Ruby, where I would have done something like this: url = "http://twofishcreative.com/" resources = %w[one two three] resources.map! { |resource| url + resource } puts resources The beauty of this is that it keeps the callback code with the calling code, making it easier to read. PHP doesn't have blocks, but it ...
[read more]
PHP's errors have always struck me as being particularly impenetrable. There doesn't seem to be any explanation of the errors on the PHP site either. What the hell does Parse error: syntax error, unexpected ';', expecting T_FUNCTION in blah.php on line 145 mean? Well, the most likely explanation for that error is that you forgot the close brace on a class, so you have a function close brace immediately followed by ?>.
I previously dismissed PHP's alternative syntax for control structures, but after spending some time working on themes for WordPress and Habari, I've come to realise that it's actually very useful from a readability point of view. The point is that if you have a mix of code and HTML, as themes do, then it can be very difficult to work out what control structure that lone dangling close brace is actually closing. By spelling it out with a endif, endwhile or endforeach the code is made just a little bit clearer. So, for templating, okay, I'll accept it. ...
[read more]
Reading through the WordPress source, I found some syntax I didn't recognize. if ( !function_exists('wp_set_current_user') ) : function wp_set_current_user($id, $name = '') { global $current_user; if ( isset($current_user) && ($id == $current_user->ID) ) return $current_user; $current_user = new WP_User($id, $name); return $current_user; } endif; A hunt revealed that this was an example of PHP's alternative syntax for control structures. You can replace an opening brace with a colon on some control structures, the closing brace with an end*;, where * is the control structure, and have blocks of multiple lines. Why would one use this? One example in ...
[read more]
In a previous post I talked about getting HTTP authentication working when PHP is intalled as a CGI. That, however, begged the question; why install PHP as a CGI? Performance is going to take a substantial hit, after all. The answer I got from the sys admin of one of the servers I was trying was:
- Better security If a PHP process is compromised, it's only compromised for the duration of the request. - Upgrades are easier Upgrading an Apache module requires restarting the Apache server after the module has been replaced. Upgrading a CGI binary simply requires replacing ...
[read more]
So, WordPress 2.3 beta adds support for AtomPub. All good. I installed it (separately to this blog, I'm just playing around), but all I could get out of the APE was a 401, even though I'd provided the correct authentication credentials. Looking at the code, with liberal use of the logging therein, I worked out that PHP_AUTH_* weren't being set, so I pulled out some auth code and tried it on it's own. No luck. Weird. I then grabbed a previously working snippet and tried that, but it was broken too. On both the servers to which I have ...
[read more]
In PHP 5.2 the __toString() method is used to convert an object to a string. In PHP 5.1 the __toString() method is used to convert an object to a string but only when print or echo is called on the object. So, doing something like this won't work in 5.1: foo = bar('passing a string to a function '.$object.' won't convert the object to a string');
I'm trying to build PHP against the MySQL supplied binary on my Mac mini running 10.4.9. For some reason it's looking in the wrong place for libraries, so I get the following error: /usr/local/mysql/lib/mysql/libmysqlclient.15.dylib Referenced from: /Users/michael/build/php-5.2.3/sapi/cli/php Reason: image not found /usr/local/mysql/lib exists and contains the libraries. It seems like a horrible fix but the following "fixes" the problem: sudo ln -s /usr/local/mysql/lib /usr/local/mysql/lib/mysql
When you save an object to a session in one file, say write.php, and then retrieve it from another, say read.php, PHP needs to know about the class definition so that it knows how to unserialize it from the session. If you don't include the class in read.php, you'll get an error complaining about __PHP_Incomplete_Class.