Higher Order PHP
sigusr2.net
sigusr2.net
Futher comment shall wait until after my first hand dabbling in a new approach: In my next non-mission-critical PHP web hobbyist app.
How come? I must have missed the memo...
map = lambda f, a: [f(x) for x in a]
filter = lambda f, a: [x for x in a if f(x)]This article about partial application may also be of interest to PHP hackers with an interest in functional programming.
function lambda( $code )
{
if( !preg_match( '/return/', $code ) )
{
$code = preg_replace('/^(.*;(?!\s*$))?/s', "$1 return ", $code);
}
$code .= ';';
$response = create_function('$_=null,$_1=null,$_2=null,$_3=null,$_4=null,$_5=null', $code);
if( !$response )
{
error_log("lambda($code) failed");
}
return $response;
}
This converts: $func = create_function('$v','return htmlenties($v,ENT_QUOTES);');
array_map($func, $data);
Into: array_map(lambda('htmlenties($_, ENT_QUOTES)'), $data);What they need to NOT do is ridiculous things like this http://news.php.net/php.internals/41374.
However, I fail to see how this will improve the overall quality of PHP applications, nor how it is a return to "the basics," or how it will help maintain the myriad content management systems that are currently out there. Care to elaborate?
I dunno, I guess I just think a functional paradigm could offer a faster and easier way for retooling existing PHP applications/frameworks that focus on what the application is supposed to do rather than how it's going to do it. Let PHP's extensive library worry about the system's protocol; this could help leverage your end-product as an API first, rather than opting-out of any further abstraction just to get what you think you want.
I dunno, I could be way off; I just think PHP is so matter of fact that attempting anything as abstract as what a purely functional language is capable of is nearly inexpressible.
For open-source projects like what I'm working on, our need to support a wide variety of hosting environments means that we took flack even just dropping compatibility for PHP 4 and 5.0.
I would love to be able to start using some of the newer/better features arriving in 5.3, but sadly, PHP 6.1 will likely be out before I can ever start using those features in public projects... :(
But, you're right. Significant changes in incremental releases like this makes it difficult for people to adopt, and I doubt that someone will bother backporting closures to a PHP 5.2.x release, like might be done in something like Python (from __future__ ... )
As for PHP 4 support, I thought that even Zend was done supporting it? Why is it still around?
Because there are still some applications and frameworks that do not support PHP4, either because no-one is working on them, or because certain incompatibilities between 4 and 5 would cause too much of the codebase to need to be rewritten. Sad but true.
Personally, I'm really looking forward to late static binding in 5.3
Hopefully this stable release will have fewer issues than 5.1, and we can all upgrade quickly. I'm getting a little tired of cursing PHP's lack of lexical closures everyday (and I'm sure my co-workers are tired of listening to me complain).
$var = $tom || 'ed';Do you mean:
$var = $tom ? $tom : 'ed';
If so, you can also do: $var = $tom or $var = 'ed';
If you don't want to calculate $tom twice.More generally, it's just strange to me that || and && don't operate the same in PHP as they do in every other C-style language. Neither does the ternary if, for that matter.
$var = $tom ?: 'ed';