|
php
sklar.com/blog
|
Wednesday, November 14. 2007
How many different ways (in PHP) are there to concatenate the string values in two variables and put the result in a third variable?
Here are a few to start:
$alice = $bob . $charlie;$alice = "$bob$charlie";$alice = sprintf('%s%s', $bob, $charlie);ob_start(); echo $bob, $charlie; $alice = ob_get_clean();$alice = implode('', array($bob,$charlie));
I would say something like "Of course, this entire exercise is just for fun and in practice is totally useless," but whenever I start out thinking that something actually productive eventually emerges. So perhaps this is totally useless, perhaps not!
Thursday, November 8. 2007
Friday, October 12. 2007
The slides from my API design talk yesterday are available.
If you missed Zendcon, come to the DC PHP Conference in a few weeks. I'll be talking about API Design there on November 8.
Thursday, October 4. 2007
As Diego and Gina have pointed out, the Ning Platform has just celebrated its second birthday.
The past two years have been a lot of work and a lot of fun. It's been gratifying to see the crazy, heartwarming, innovative uses folks have come up with for the platform and also extremely rewarding to work with such talented people.
I wonder what I'll have to say in a blog post a year from now that links to this one?
Monday, September 24. 2007
Zendcon (aka "The 2007 Zend/PHP Conference and Expo") draws near. I'll be there giving a talk about API Design in PHP and undoubtedly getting sucked into various conversations about how PHP's disorganized ugliness is its greatest asset.
Thursday, July 12. 2007
+1 to what ndg said in response to Jacob's post about e-mail address validation.
I think Jacob sort of touches on this in his post but too often it's not explicit what people are looking for under the vague and broad umbrella of "e-mail validation".
If it's "Did the AOL user signing up for my site forget the '.com' part at the end of the address?" then some pretty simplistic text-based matching will do.
If it's "Is the address supplied a valid mailbox that is controlled by the person who is supplying it to me?" then all the string parsing in the world isn't going to save you. You'll need to roundtrip something out to the mailbox with confirmation instructions. And even then you're not out of the woods -- maybe the user's mailbox is full, or a transient DNS error is causing a problem.
I think e-mail address validation tends to be a bit of a tarpit for nerds (myself included) because the delicious complexity of RFCs 822 and 2822 makes writing code to handle the grammars they describe a fun challenge. That makes it easy to forget what the point of the address validation in the real world actually is: handholding well-meaning users that may have made a careless mistake, or robust protection against malicious users, or bot protection (in which case you probably want to ban RFC-valid addresses that have known-suspicious domain names or components), etc.
Wednesday, July 11. 2007
I'm going to be giving a talk on "API Design in PHP" at ZendCon 2007 in October.
Our PHP API at Ning is a little over two years old at this point. There are some things I love about it that I think we've done really well, some things that we have revised, some things we can do better, and some interesting things planned for the future.
The talk is all about what we've learned developing, building, and supporting the API with both a ferocious devotion to backwards compatibility and a rapid new release cycle.
See you in October!
Wednesday, May 16. 2007
A PHP issue that comes up over and over again is how the static keyword doesn't know about inheritance. That is, code such as:
class Masons {
static $where = "World-wide";
static function show() {
print self::$where;
}
}
class Stonecutters extends Masons {
static $where = "Springfield";
}
Stonecutters::show();
prints World-wide, not Springfield because the self inside Masons::show() is bound at compile time to the Masons class. This is different than how $this works in instances, so it can be unexpected.
There are plenty of good reasons why PHP 5 works this way and it seems that in PHP 6 the static keyword will be able to be used in place of self to get the dynamic behavior a lot of folks are looking for (that is, print static::$where; in Masons::show() would cause Stonecutters::show() to print Springfield.
All well and good once PHP 6 is done.
In the meantime, I was noodling around with runkit and came up with some glue that lets you do something like this:
class Model {
public static function find($class, $filters) {
// This method would actually do an SQL query or
// REST request to retrieve data
print "I'm looking for a $class with ";
$tmp = array();
foreach ($filters as $k => $v) { $tmp[] = "$k=$v"; }
print implode(', ', $tmp);
print "\n";
}
public static function findById($class, $id) {
return self::find($class, array('id' => $id));
}
}
class Monkey extends Model { }
class Elephant extends Model {
public static function findByTrunkColor($color) {
return self::find(array('color' => $color));
}
}
And then after the mysterious (for a few seconds) glue:
MethodHelper::fixStaticMethods('Model');
you can do:
Monkey::find(array('name' => 'George', 'is' => 'curious'));
// prints: I'm looking for a Monkey with name=George, is=curious
Elephant::findById(1274);
// prints: I'm looking for a Elephant with id=1274
Elephant::findByTrunkColor('grey');
// prints: I'm looking for a Elephant with color=grey
Monkey::findById('abe');
// prints: I'm looking for a Monkey with id=abe
(The innards of MethodHelper after the jump...)
Continue reading "Runkit, "static", and inheritance"
Wednesday, April 25. 2007
So I've got this string (in PHP) and I need to scan through it character by character. I can't scan byte by byte because it's 2007, our users write in all sorts of languages, and the string is UTF-8.
The PHP 5 solution uses mb_strlen() to find the length and then mb_substr() to grab each character:
$j = mb_strlen($theString);
for ($k = 0; $k < $j; $k++) {
$char = mb_substr($theString, $k, 1);
// do stuff with $char
}
In PHP 6, one would do:
foreach (new TextIterator($theString, TextIterator::CHARACTER) as $char) {
// do stuff with $char
}
Some rough benchmarks on a 1500 character (and 2900 byte) string (Linux, whatever processor is inside this Thinkpad T43 here, your mileage may vary, etc etc etc) give me about 61 scans/sec with PHP 5.2.1, where a "scan" is just moving through the loop above with mb_substr and doing one if() test comparing the char to '<'
Under PHP 6.0.0-dev with unicode.semantics=on, switching from mb_strlen() and mb_substr() to regular strlen() and substr() produces about the same result. And indexing with $theString[$k] is the same speed as substr().
However, the TextIterator case is much faster, about 450 scans/sec!
Nicely done!
Friday, November 3. 2006
This story from George via Andrei is simultaneously hilarious and scary.
I'm not sure, if I were a PHP function, which function I'd be. Although figuring that out would be a lot easier if my name were, for example, Max Levenshtein.
Thursday, September 28. 2006
It's been a lot of hard work, so I'm quite excited that we've just released three great new Ning sites: Ning Videos, Ning Photos, and Ning Group.
I particularly like the embeddable slideshow that Ning Photos has, and its companion in Ning Videos, the embeddable player -- so you can put photos or videos on your blog or wherever. Both apps let you e-mail in content from your phone, too. Ning Group has some spiffy HTML parsing and file upload features so you can share documents with folks and incorporate music, pics, or anything else in the forums.
Plus, all three sites have the juicy bits that every site on the Ning platform gets -- things such as cloneability, complete customization, and built-in REST APIs. I've been watching the feeds for clones of photos and videos -- I suppose seeing who's cloned sites you care about is the Web 2.0 version of ego surfing.
More on the Ning Blog and from Kyle.
Tuesday, August 29. 2006
The new edition of PHP Cookbook is on the way! I got one copy yesterday, so it should be making its way into bookstores and online-bookstore-warehouses any day now.
There is lots of new material in this edition -- completely revamped XML and OOP sections, new stuff on PDO, Ajax, testing, performance tuning, regular expressions, and lots of other goodies.
Thursday, August 24. 2006
Some neat Ning + PHP related stuff recently: Ben and Elizabeth set up a Group clone for PHPCommunity -- http://phpcommunity.ning.com.
Ben also set up an app -- http://zendfw.ning.com -- where he installed the Zend Framework and made a few tweaks so it's runnng happily on the Ning Playground. I was pleased to see that our URL mapping support can handle everything that Zend Framework needs.
Tuesday, July 25. 2006
I'm heading to OSCON today. Things I'm looking forward to (in no particular order): Portland, giving a new talk, seeing friends, learning lots of new things. If you're at (or going to) OSCON, say hi and tell me how wonderful (or stinky) you think my blog is.
Monday, July 24. 2006
XMLHttpRequest Quirks and PHP has some tips on making HTTP requests from Javascript (and is not really PHP-focused, despite the title). The brief discussion of the benefits and drawbacks of using XML, HTML, or JSON for data exchange is worthwhile, and I suppose there's nothing wrong with any of the info about the XMLHTTPRequest object, but I think if you're doing any moderately serious Javascript stuff that requires HTTP requests (see what wonderful contortions I undergo to avoid saying "Ajax"! Oops.), you've got problems if you're interacting with XMLHTTPRequest (or the IE-equivalents via "new ActiveXObject()") directly.
Instead, use a library such as Dojo or Prototype. There are a lot of subtleties to making requests work properly -- things such as cross-browser support and using different request transports based on the kind of data that needs to be sent to the server. If you start down the road of trying to do all that yourself, you'll go nuts (or at a minimum, waste your time reinventing). If you ignore all of those subtleties, your app won't work correctly.
So take advantage of the hard work someone else has already done to solve these problems. We may argue over whether real programmers use emacs or vi or Microsoft Visual InterDev 2006 for .NET Enterprise Architect Edition, but there's not much support for the "I wave a tiny magnet back and forth just-so next to the hard drive" method of editing these days. Avoid waving the tiny magnet to make HTTP requests from Javascript.
|
|
|