Min-width and Max-width template
Min-width and Max-width template
Some problems seem to appear again and again, and one of them is page width. How wide should a site be? Should you adapt to 800px resolution? 1024px? Fully fluid with percentages? Perhaps elastic using em-units? There are lots to choose from. What I’ve found is that there’s one solution that almost always works. You […]
The state of functional programming in PHP
With the rise of Javascript, and languages like Python and Ruby, functional programming is becoming more mainstream. Even Java seems to be getting closures in the next version, so does this leave PHP lacking behind or is there an unrealised potential hidden within? Dynamic dispatch What exactly defines a functional programming language, is perhaps an open question, […]
With the rise of Javascript, and languages like Python and Ruby, functional programming is becoming more mainstream. Even Java seems to be getting closures in the next version, so does this leave PHP lacking behind or is there an unrealised potential hidden within?
Dynamic dispatch
What exactly defines a functional programming language, is perhaps an open question, but one key element is functions as data. As it happens, PHP kind-of-supports this concept; The syntax permits you to use variables as function-names, making the following possible:
function add($a, $b) { return $a + $b; } $add = "add"; $add(2, 8); // return 10
Unlike languages with first class functions support, the variable $add isn’t a special type — It’s merely a string, which is evaluated in the context. It may just be a wrapped up eval, but superficially it works similar, once the function has been declared.
It is also possible to explicitly call a function reference with call_user_func. This is interesting, because it accepts different types of arguments, which makes it possible to call a method on an object. More on this in a moment.
Binding state
Another prerequisite for functional programming, is the ability to bind a variable to a function. This bound function serves essentially the same purpose as objects do in object oriented programming, but is usually more fine grained and more flexible.
In languages, which are traditionally associated with functional programming, functions are usually bound with variables, through something called a closure. This is a side effect of the scoping rules of those languages.
Since PHP doesn’t have lexical scope, we can’t use closures, but we can use currying to achieve the same goal. In Wikipedia’s words, currying is the technique of transforming a function that takes multiple arguments into a function that takes a single argument. If that sounds abstract, assume the following:
$add2 = curry($add, 2); // returns a new function reference, to add(), bound with 2 as the first argument $add2(8); // return 10
Before you try that out, hold your horses — it won’t work, because curry isn’t a PHP function. As it turns out though, it’s possible to create it. Sort of.
Implementing curry
Without going into great details, there are two ways, currying can be implemented in PHP.
- The first option is some sort of run-time evaluated code. This could be variable-variables, pointing to function names or callback pseudotype, invoked by call_user_func. Eventually, they are just kinds of eval.
- The other option is to use a command object, encapsulating the callback and its state.
For a more detailed explanation, have a look at Partial function application in PHP.
Weighing the options
Looking at the syntax, variable-variables have a much more functional “feel” to them, than a command object. The callback pseudotype does allow one to reference an object + method-name, but even though this is supported by PHP’s internal functions, it get’s bulky in user land code. If a command object feels like functional programming with OOP syntax, then call_user_func feels like functional programming with procedural syntax.
When it comes to performance, the run-time evaluated approach has some serious shortcomings. Currying must be done by creating new static code each time and this can’t be reclaimed until the end of the process. Furthermore, the only way to bind state, using this pattern, is to rely on a global container of some sort, generating a new, unique symbol each time. Since it’s impossible to know, when the callback isn’t referred anymore, it follows, that it’s impossible to safely remove the variable from the global container. Thus, any state bound this way, will be impossible to reclaim for PHP’s memory management system, furthering the risk of the script running out of memory.
In conclusion
So, if the offset for this post was to assert the current options for functional programming in PHP, the only practical solution is currying, using command objects. The awkwardness can be somewhat smoothed out, with a library of utility classes. phunctional is an attempt at this and we might also see some of these ideas emerge into more general purpose frameworks.
Another idea, could be a language level addition to PHP. What we need, is a way to make all callbacks callable with the variable-variable syntax. I’m thinking this could be supported with a magic-method. If an object was to be used as a function, and it implemented said magic method, the method would be called. From a design perspective, it would fit fine with the other magic methods, such as __call and friends.
Declaring functions
Another limitation, which could be addressed at the language level, is the matter of declaring functions in the first place. Currently, functions must be declared in the global scope or through the use of the hideous create_function. There have been some stabs at improving this, on the php-internals team, so maybe we should be as lucky as to see this in PHP 6?
This article provided by sitepoint.com.
Web Content: Not just YOUR words and pictures
If they read what you write, they may also want to read what you read. Webmasters and bloggers know this. That’s why we’ll embed links within our text, build pages with links to recommended sites and/or add linkblogs to our side bars. In a world where most of us don’t have the time to research and write everything we’d like to share, such resources add value to our existing content and give readers guidance on where to look for additional information.
If they read what you write, they may also want to read what you read. Webmasters and bloggers know this. That’s why we’ll embed links within our text, build pages with links to recommended sites and/or add linkblogs to our side bars. In a world where most of us don’t have the time to research and write everything we’d like to share, such resources add value to our existing content and give readers guidance on where to look for additional information.
Recently I’ve come across some other good ways to share what you read, so I thought I’d share those with you today.
Publish your OPML file to share your blog subscriptions
A few weeks back I was reading an entry on David Bradley’s blog, Sciencebase, when I noticed something interesting in his footer. There, at the bottom, he has a section called “Geeky Fun Stuff” in which he shares, among other things, a link to his OPML file. That, I thought, is a really good idea. For those of you who don’t know what this is, an OPML file is basically a type of XML file that includes the links to the RSS feeds of the various blogs one reads through RSS Readers such as Google Reader, Bloglines, etc. Such services allow you to import and export these files so that you can easily switch services or add a batch of feeds to your existing service. Thus, if I wanted to subscribe to all of David’s feeds I could just save that file and import it into Google Reader myself. Or if I wanted to subscribe to only a few I could edit the file (in Dreamweaver or any plain text editor) to delete any I didn’t want.
If you are already using an RSS reader, sharing such a file is fairly easy. Just export your file from your reader and save it to your computer. If you don’t want to share everything, just open the file in a text reader, and delete the extraneous feeds—lolcats, curling news from In the Hack and anything else that may not be of interest to your readership. Once the file is ready, just upload it to your site and link to it as you would any other page.
Using Google Reader to share specific stories
Google Reader recently added some enhancements to its sharing features. I first noticed this when Robert Scoble posted a note on Pownce with a link to his Google Reader shared items page. When I went to view the page I realized that this could be a useful feature, one that made me want to revisit Google Reader.
When you visit a shared items page you will see a site that looks pretty much like a typical blog. Stories are posted on the left, information about the page owner is on the right—along with links to other resources, a feed, etc. The main difference is that the stories are things the page owner has read rather than written (though, if you subscribe to your own feed, you can share your own entries as well). Each story also includes a link to the original entry and the original source—so the material is not mistakenly attributed to you.
After viewing Scoble’s page, I immediately thought of my friend X, who says she wants to establish an online presence, but isn’t quite yet ready to blog. Sharing stories on a page she can link to might be a good way to get her feet wet and let people know what she is thinking about.. For those of us who already have one or more blogs and Web sites, the shared items page adds to our online mix and provides an easy way to share stories with our readership.
Getting started with Google Reader
Getting started with this is pretty easy. Just go to the Google Reader site and sign-in. If you don’t already have a Google account for Gmail, analytics, etc. you can create one there. Once your account is set up, just subscribe to some of your favorite blogs and start reading. A menu at the bottom of each story gives you the option to share the story so it will appear on your public shared items page. (There is some controversy about this, but you just have to understand that it’s a public page that can be seen by anyone who has, or discovers, the address. For us, that is what we want, so it’s not a big concern.) If you change your mind later, you can unshare the story the same way. You can also organize your subscriptions into topic folders and share topics rather than individual items. To learn more about using Google Reader visit the Reader Help Center.
If you don’t want to send readers to your Google page, but still want to share stories, you can also share a clip from the feed on your own site, as I have done on my “What I’ve Been Reading in the Blogosphere” page.
More sharing options
Google isn’t the only service that allows sharing, but aside from the hubbub regarding privacy settings (pertaining to how and with whom one is sharing—see links below) it’s very easy to use and will be familiar to a large audience. I’ve been sharing blog stories with groups on Streamy since last summer, but my Streamy shares aren’t fully public. Another friend has recently recommended Feed Each Other which looks promising and also produces a public page. StumbleUpon, while not a reader, is also a great way to share blog stories and other Web sites. (Stumbling is quite popular with insomniacs and is a great way to learn about other sites.)
These are all useful services, but how you share is less important than what you share. If you can find articles and sites that offer additional information on the topics you discuss, or even stories that add insight to your personality or world view, you’ll be providing a helpful resource to your readers.
OPML, Google Reader and Sharing Resources
- fav.or.it - favorit RSS Reader and Blogging Platform
- Google’s new Reader Features
- Google Reader needs GPC (Granular Privacy Controls)
- Google Reader “Share With Friends” Feature Gets Privacy Complaints
- Google Reader Sharing FAQ
- Is Google Reader Sharing Too Much?
- OPML (Outline Processor Markup Language)
University Main Web Homepage On-line Discussion
University Marketing and Communications is seeking additional community input as it continues its multi-year process of reviewing the university’s main homepage, which was introduced, with slight variations, along with the new university logo in 2004.

Current Homepage
University Marketing and Communications is seeking additional community input as it continues its multi-year process of reviewing the university’s main homepage, which was introduced, with slight variations, along with the new university logo in 2004.
In the next of what will be several phases of input from the university community, University Marketing and Communications is hosting an opportunity for faculty, staff, students, alumni and friends of the university to offer ideas on-line through this blog and a forum discussion.
University Marketing and Communication asks that the university community provide thoughts around these three questions:
- What are the two or three most important objectives of a university Web homepage?
- Who are the two or three most important audiences for a university Web homepage?
- What kinds of content would best meet those objectives and reach those audiences?
Thank you for your insights. We’ll be accepting feedback on this blog through September 7, 2007. For additional information on the homepage project, go to http://www.case.edu/webdev/homepage.
Paula Baughn, on behalf of University Marketing and Communications
Now that the feedback period has ended, comments on this entry are now closed. Thank you for your input.
New design for friendlybit coming up
Hi. I just wanted to tell you that I’m now working on the new friendlybit. I’ve listened to your previous comments, and these are the changes I’m going to make/not make: Blog format stays, no community. You’re damn conservative (something like 95% percent said to keep it a blog). More compact writing. I’ll skip more of the […]

