PHP6 gets a COMEFROM statement

By | | PHP

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?

Written By:

Harry Fuecks

Harry has been working in corporate IT since 1994, with everything from start-ups to Fortune 100 companies. Outside of office hours he runs phpPatterns: a site dedicated to software design with PHP that aims to raise standards of PHP development. He also maintains Dynamically Typed: SitePoint's PHP blog.

 

{ 27 comments }

lsd July 29, 2008 at 7:31 pm

may be

seodevhead April 7, 2006 at 12:04 pm

I remember in my first computer science class, unbeknownst to all, I wrote an entire program using goto’s and not a single function. Looking back, I was the first one to finish the perfectly working program, but then again was the first to receive an F. I thought I was sooo cool with my newfound statement, the GOTO.

XoloX April 4, 2006 at 6:02 pm

Thank god

RaS! April 4, 2006 at 4:15 am

GOTO sucks

nauhygon April 3, 2006 at 10:30 am

when i first saw the post, i was seriouly missing the ‘caller’ property in the javascript Function object and thinking if COMEFROM was of the same nature. harry, nice one! :)

spinmaster April 3, 2006 at 10:13 am

nice one… ;) you got me there!

Nova April 3, 2006 at 4:26 am

hehe, nice one

Ryan Wray April 2, 2006 at 10:26 pm

@_theworks:
It is was a well-constructured April fools joke. :)

_theworks April 2, 2006 at 10:05 pm

Ok this is confusing? are they really adding GOTO and COMEFROM ? or was this an april fools joke?

Dean C April 2, 2006 at 6:41 pm

Ah you’re evil Harry – you got me here :(

Gator99 April 1, 2006 at 10:49 pm

Great news, when is the Gosub tag coming out, php 6.1?

mx2k April 1, 2006 at 4:27 pm


sorry i’m used to the forum tags

mx2k April 1, 2006 at 4:26 pm

that will be the next php egg,

[code]

[/code]

[code]
output: harry says hi
[/code]

Anonymous April 1, 2006 at 3:57 pm

and I was remember my AS/400 RPG days of the goto

Except the GOTO part isn’t a joke – haven’t seen exactly how it’s going to work but it looks like it will be a way to jump between labels inside a switch statement .

ajking April 1, 2006 at 3:20 pm

Never mind COMEFROM. Having looked at the INTERCAL code, I think it’s time we put the “Please” statement in PHP. Especially for debugging, as in “Please let this work!” :-) ROTFL, Harry, great post.

jplush76 April 1, 2006 at 3:00 pm

dammit, I have to start realizing today is april fools day I totally thought you were serious and I was remember my AS/400 RPG days of the goto and it was a nightmare, whew
ya got me :)

mkrz April 1, 2006 at 12:23 pm

Damn, you got me! :) Definitely one of the better April jokes I’ve seen.

Maarten Manders April 1, 2006 at 5:18 am

By the way, i love this INTERCAL code. ;)

Dr Livingston April 1, 2006 at 4:49 am

damn it… you got me :lol:

Dr Livingston April 1, 2006 at 4:47 am

sorry harry, i’m not convinced…

goto? comefrom? wtf?? what are they doing to our language huh :(

HarryF April 1, 2006 at 4:40 am

Note to self: remember what time zone I’m in next time

Maarten Manders April 1, 2006 at 3:38 am

Finally! ;)

jayboots March 31, 2006 at 10:12 pm

Wow, I’m impressed at the level of detail you went to for this joke. I was smirking at the very mention of comefrom but laughed outloud at: “INTERCAL, a language which, for various reasons never hit mainstream”.

Good one!!

Octal March 31, 2006 at 9:22 pm

Nice one Harry :D

ivanv March 31, 2006 at 8:24 pm

Oh, it’s still 31 here, LOL

Seinfeld March 31, 2006 at 8:13 pm

1st of April :D

ivanv March 31, 2006 at 8:13 pm

I think you meant “open the gates of HELL”… and a flood of oni aliens will come down upon us.

sigh…

Comments on this entry are closed.

{ 3 trackbacks }