Programming
Article
By Harry Fuecks

PHP6 gets a COMEFROM statement

By Harry Fuecks

One of the more controversial additions to PHP6 is the GOTO. Some have argued it flys in the face of many years good programming sense but there are valid use cases, when running performance critical operations such as parsing, where the overhead of making PHP user function calls becomes significant.

Anyway GOTO seems to have opened the gates to some more radical language modifications and recent discussion a revolved around adding a COMEFROM statement to PHP, which led to an initial patch being applied to the PHP6 CVS branch here.

Precedent for COMEFROM can be found in INTERCAL, a language which, for various reasons, never hit mainstream but aimed at being a better LISP, as this code listing illustrates.

Like GOTO, COMEFROM could lead to spagetti if used unwisely, so the initial implementation places a sensible restriction on it’s use: you can only COMEFROM a PHP script which is not the script where COMEFROM was used. An example if I have some include file like;


<?php
// login.php - the script we want to COMEFROM
function login($username, $password) {
   $auth = new Auth();
   return $auth->isValidUser($username, $password);
}

I can step into this with COMEFROM like;


<?php
// index.php
require_once 'login.php';

class MyAuth implements LoginHandler {
    // Some other implementation providing the same interface
    function login($username, $password) {
        // authenticate against, say, an LDAP server
    }
}

// Execute a COMEFROM, replacing use of Auth class with MyAuth
COMEFROM 'login.php:3' [
   $auth = new MyAuth();
   return $auth->isValidUser($username, $password);
]

if ( login($_POST['username'], $_POST['password'] )) {
   echo "You are logged in<br />";
}

As the above code illustrates, this gives you the possibility to override the behaviour of code without having to physically alter it or worry about stuff like dynamic includes. What this trivial example doesn’t illustrate is some of the more advanced things COMEFROM enables, such as functional programming, macros and Ruby-like “blocks”, all of which make PHP significantly more dynamic. That said, at the moment, what I see as missing is a way to return back into the code you have COMEFROM, which would allow for stuff like an extremely fast AOP implementation – perhaps GOTO could be extended to support this?

Anyway – could this be the feature that drives demand for hosts to migrate to PHP?

Recommended
Sponsors
The most important and interesting stories in tech. Straight to your inbox, daily. Get Versioning.
Login or Create Account to Comment
Login Create Account