CAP
October 16th, 2010Because I love making stupid little libraries I’m proud to present CAP, “CAP is an experiment is stateful PHP form handling.”
As you can see, closures are bound to form elements. Pretty cool, no? PHP 5.3+ only.
Because I love making stupid little libraries I’m proud to present CAP, “CAP is an experiment is stateful PHP form handling.”
As you can see, closures are bound to form elements. Pretty cool, no? PHP 5.3+ only.
PHP 5.3 has added support for true anonymous functions and closures. Unfortunately, there are a few gotchas.
The main one is that no variables are automatically enclosed: you must specify the variables you want to enclose. So, this will cause an exception:
$a = 2; $f = function($i) { return $i + $a; } $f(1); // -> PHP Parse error: syntax error, unexpected T_VARIABLE
While this is the proper format:
$a = 2; $f = function($i) use ($a) { return $i + $a; } $f(1); // -> 3
However, you can also cheat with global:
$a = 2; $f = function($i) { global $a; return $i + $a; } $f(1); // -> 3
Also, classes that are in scope seem to get imported into the closure’s scope. I believe this is because the class isn’t actually copied into the closure but is instead looked up:
class C { static $a = 1; } $f = function($i) { return $i + C::$a; }
While normally you would never want to abuse the scope, I guess there may be times when you do.
You can even pass enclosed values by reference:
$a = 2; $f = function($i) use (&$a) { $a = $i; } $f(1); $a == 1 // -> true
Interestingly, I also learned that you can’t mutate a function’s input value:
$f = function($i) { return $i++; } $f(1); // -> 1
This is also the case in Javascript. To be honest, I’m not sure why this is, since the parameter is not an enclosed variable. But there you go. I’m sure someone who knows more about closures than me can explain why this is.
PS I find the original closures RFC somewhat more useful than the main documentation.
I’ve been doing a bunch of PHP programming the last few weeks for a client and it’s been such a pain to use PHP’s archaic array_* methods. And don’t get me started on having to return arrays like array('result' => 'not_ok', 'message' => 'User not found') to indicate success or failure. I had some time to kill at the airport yesterday, so today I am happy to bring you BFCollections. The collections included are:
If there’s interest I’ll see about improving them and even adding additional collections.
PHP does some funny things when typecasting strings as integers and may not work the way you would expect (to be fair, it is documented). Here are some examples running under PHP 5.2.5 from the Mac OS X command line:
php -r 'var_dump((int) "agbae");' int(0)
php -r 'var_dump((int) "zzzzzzzzzzzzzzzzzzzzzzz");' int(0)
php -r 'var_dump((int) "2agbae");' int(2)
php -r 'var_dump((float) "42165.6agbae");' float(42165.6)
This is important to know because PHP allows you refer characters of strings just like you would the elements of an array (and somewhat similar to C’s strings):
php -r '$s = "abcdef"; var_dump($s[2]);' string(1) "c"
However, giving an invalid key won’t given an error, depending on how your installation of PHP is configured in php.ini. If you give an out-of-bounds string you get an empty string:
php -r '$s = "abcdef"; var_dump($s[45]);' PHP Notice: Uninitialized string offset: 45 in Command line code on line 1 string(0) ""
If you give a string key, it is always for the zeroth index, since it is typecasted to an int and typecasting works as described above:
php -r '$s = "abcdef"; var_dump($s['zzzzz']);' PHP Notice: Use of undefined constant zzzzz - assumed 'zzzzz' in Command line code on line 1 string(1) "a"
This is the first in a series of posts on technical issues related to web site development and making user-friendly websites. These will focus on the technical details of developing web sites and applications so will probably be interesting to only a subset of readers. Within the next few months, time permitting, we will be launching a new section of the site called Bubble Foundry Labs, where you will be able to find both technical articles and experimental web applications. For non-technical types, these demo applications may prove easier to understand than this article series.