RSS ? Recent Blog Posts

Blogs » PHP
 

Dynamically Typed

: PHP Blog

Rakish resolution - more flame

by Harry Fuecks

Happy 2008! At the risk of being a cad (or just plain annoying and silly)…

Zed Shaw, (ex-?)author of Mongrel, makes this remark in his end of year epiphany Rails is a Ghetto;

(15:11:12) DHH: before fastthread we had ~400 restarts/day
(15:11:22) DHH: now we have perhaps 10
(15:11:29) Zed S.: oh nice
(15:11:33) Zed S.: and that’s still fastcgi right?

This one single statement [from 2007-01-20] basically means that we all got duped. The main Rails application that DHH created required restarting ~400 times/day. That’s a production application that can’t stay up for more than 4 minutes on average.

Let me put this into perspective for you: I’ve ran servers that needed to be restarted once in a year. They were written in PHP, Python, Java, C, C++, you name it. Hell, I’ve got this blog on a server I’ve restarted maybe 10-20 times the whole year.

Now, DHH tells me that he’s got 400 restarts a mother f***ing day. That’s 1 restart about ever 4 minutes b****es. These restarts went away after I exposed bugs in the GC and Threads which Mentalguy fixed with fastthread.

If anyone had known Rails was that unstable they would have laughed in his face. Think about it further, this means that …

 

Lexical scope to appear in PHP?

by Troels Knak-Nielsen

I mentioned briefly, in my last post, that Wez Furlong made a patch back in March, that allows a native syntax for creating anonymous functions. This could replace the heinous create_function.

Since then, I brought up Wez’ original patch again on the php-internals lists. This has prompted some hefty debate over the last week. The main argument against approving the patch, seems to be, that one would expect static scoping rules to apply to the anonymous function. After all, this is the case in similar languages, which support anonymous functions. One could only assume, that changing PHP to support this, would be a major undertaking.

One would be wrong, it seems.

Today, Christian Seiler posted a patch to allow lexical scope for anonymous functions. There are a few loose ends, but it appears to work in general. Instead of making all variables follow the static scoping rules, a new keyword (lexical) is introduced. It works similar to global, in that it must be explicitly declared, which variables are lexically scoped. This is how it looks in action:

function getAdder($x) {
return function ($y) {
lexical $x;
return $x + $y;
};
}
$add2 = getAdder(2);
$add2(8); // return 10

It’s …

 

The state of functional programming in PHP

by Troels Knak-Nielsen

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 …

 

PHP Manual CLI style 2.0

by Troels Knak-Nielsen

Harry mentioned the handy little phpm some three years ago. And Sean Coates was kind enough to point out how it could be replaced with a shell one-liner. Doesn’t that just make one love bash?

One thing, I missed with either of the two, was the ability to see the entire manual entry. It’s quite often, that the manual actually holds useful information (Who’d known that!), so I find myself using www.php.net a lot. Or I did, until I decided to do something about it. Now, shell-scripting isn’t what I spent most of my time on, so it’s not with out a bit of pride, that I present to you phpm two-oh.

#!/bin/bash
# phpm
# commandline php-manual interface
# Kudos to Havard Eide and Sean Coates for the original idea
#
# author: Troels Knak-Nielsen <troelskn@gmail.com>
# version: 2007-11-27
#
# dependencies:
# wget sudo apt-get install wget
# sed sudo apt-get install sed
# tidy sudo apt-get install tidy
# xmlstarlet sudo apt-get install xmlstarlet
# konwert sudo apt-get install konwert
# html2text …

 

Index of PHP tokens for Emacs and beyond

by Troels Knak-Nielsen

I’ve been using Emacs as my primary editor for a while now. A lot of people prefer IDE’s, but I’ve never been comfortable with them. I kind of like the ability to show a list of classes & functions in a file though. Emacs can use ctags to generate a list of tokens for a file, but I weren’t really satisfied with its output.
As you probably know, PHP has a ridiculous amount of functions for all kinds of things, and as it happens, token_get_all gives access to the Zend Engine tokenizer. In other words, the same chunk of code, which PHP itself uses, when reading a .php file. This provides an excellent base for writing a script, which can parse the socks off ctags.

So without further ado: ~/scripts/tokens.php parses a file and prints the classes and functions found in there. It also prints a small excerpt of any docblocks, which precedes the item. This makes for a nice table of contents.

<?php
if (!defined(’T_UNSPECIFIED_STRING’)) {
define(’T_UNSPECIFIED_STRING’, -1);
}
function token_get_all_improved($data) {
$tokens = array();
$line = 1;
$col = 0;
$level = 0;
$scope_level = null;
$in_scope = false;
foreach (token_get_all($data) as $token) {

 

The PHP Anthology: 101 Essential Tips, Tricks & hacks, 2nd Edition

by Andrew Tetlaw

The PHP Anthology: 101 Essential Tips, Tricks & hacks, 2nd Edition is out! This is a big deal for me because it’s my first book as a technical editor. It represents several months’ work and a considerable amount of coffee. I think it’s a great book with heaps of code you can rip out and start using straight away.

One of my favorite parts is chapter 10: “Access Control”. If you read the whole chapter you’ll realize that you have a complete, ready-made user registration and permissions system, with email confirmation, groups, permission levels and session storage. You’ll find this chapter in the free sample PDF too!

And you’ll find even more answers to common problems like how to send HTML email with attachments, how to read or produce an RSS feed, how to make a customized data grid, and how to speed up your site through caching. It’s like codeine for your PHP headaches!

In the spirit of eating my own dog food, I recently used code from the book to create these slightly amusing bar graphs. They are built from the data from the 2007 Web Design Survey and they provide this shocking revelation: 50% of people in the web …

 

Wide Finder in … errr … PHP

by Harry Fuecks

Long time no blog - very busy searching Switzerland these days but popping in first to say congrats to the 2nd edition team and second, for the sheer evil delight in submitting a late PHP implementation for Tim Bray’s fascinating Wide Finder Project.

Tim set a simple, but very much real-world challenge; write an app that determines the top 10 most popular blogs from his Apache access log. It should be fast and readable, with a subtext of illustrating how “language X” copes in terms of parallel processing and utilizing “wider” (many processor) systems.

Now technically, PHP has almost zero ability to execute stuff in parallel, other than pcntl_fork - (don’t try that on your live webserver - CLI only!), unless you’re counting ticks, which I hope you aren’t.

But we’re not going to let little details stop us! Because we’ve got curl_multi_exec() which allows us to talk to multiple URLs and process their responses in parallel. And just think how many requests Apache + mod_php is able to serve at the same time. Perfect for some map and reduce…

So first a mapper;

<?php
if ( isset($argv[1]) ) $_GET[’f'] = $argv[1];

if ( !isset($_GET[’f']) || !is_readable($_GET[’f']) ) {
header(”HTTP/1.0 404 Not Found”);
die(”Cant …

 

Dynamic global functions in PHP

by Troels Knak-Nielsen

Like many others, I prefer to use procedural PHP as a template language. While PHP’s syntax makes it a practical choice for this, there is a problem with embedding dynamic content. Most PHP applications produce HTML output, so you end up writing <?php echo htmlspecialchars($foo);?> a lot, using this technique. Or you forget it, and make your application prone to all sorts of nasty XSS attacks.

Apart from the annoyance of superfluous typing, there is a danger of getting lazy, seeing that <?php echo $foo;?> is remarkably shorter to type. In some situations, it won’t manifest itself as a problem either, since some content-types never contains HTML special characters (Numbers for example). This is particularly nasty, because errors in the view layer are notoriously hard to track down, and unlike SQL-injections — a similar problem — the consequences tend to hurt the users of a site, rather than the site directly.

KISS
Recently, I had a look at some code, written for CakePHP. My eye caught a function e, which is shorthand for echo. A single letter, regular function is undoubtedly the simplest way to extend PHP’s syntax. Thinking about it, it’s fairly obvious, but it just never occurred to me.
Well, the CakePHP …

 

Do less

by Troels Knak-Nielsen

So I got persuaded into blogging for Sitepoint and this is my first post. I’ll be writing mostly about PHP, since that’s my main area of competence, but will probably touch on related topics.
Anyway, in an attempt to not turn this into yet another “Hello World”, I figured I would reflect a bit on what’s special about PHP.

PHP doesn’t do stuff

It basically boils down to one thing; PHP doesn’t do stuff for you.

Taking a look at the hordes of extensions, which comes with PHP by default — or the overwhelming array of core functions — it may appear that PHP does an awful lot of things, but it doesn’t. Not really. This functionality is mostly passive; When you look at the language core, it makes very few assumptions about the application, you’re going to build. Instead, it supplies the tools to do so.

This is the tradition of open source. Linux is renowned for having a multitude of desktop environments, shell interpreters and whatnot. PHP follows the same organisation. It does have a one pivot point though; The language itself has only one official implementation. Well, two actually, but that’s hopefully going to be fixed eventually.

PHP doesn’t have a framework ..

These …

 

Win Cash for the best Facebook App!

by Matthew Magain

Via digg: Our current feature article about developing for the Facebook Platform with PHP is extraordinarily timely, given the recent announcement from venture capital firm Bay Partners on Tuesday that they will be offering cash prizes for the best Facebook applications.

Dubbed the “AppFactory,” the program will grant awards of US$25,000 to $250,000 to Facebook developers using a fast-track approval process. The company hopes to make up to 50 such awards, Salil Deshpande, a partner at Bay Partners, told the E-Commerce Times.

Given that Facebook has just clocked 30 million members, there’s never been a better time to make your app even more social. Email your application for the AppFactory program to Bay Partners to be eligible.

 

Sponsored Links

SitePoint Marketplace

Buy and sell Websites, templates, domain names, hosting, graphics and more.

Logo Design, Web page Design and more!

99designs

  • Custom logo designs created ‘just for you’.
  • Pick the design you like best.
  • Only pay if you’re satisfied with the result.

The Web Site Revenue Maximizer

New Release

Free PDF Download:

101 Ways To Make Money From Your Website!

Free eBook! Firefox Revealed