PHP6 gets a COMEFROM statement

Share this article

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?

Frequently Asked Questions about PHP6 and COMEFROM Statement

What is the COMEFROM statement in PHP6?

The COMEFROM statement is a unique feature introduced in PHP6. It is a control flow statement that is considered the opposite of the GOTO statement. Instead of directing the program flow to a specific point, COMEFROM specifies where the control should come from. It’s a humorous and unconventional approach to programming, and its practical use is often debated among developers.

Why is PHP6 missing from the version sequence?

PHP6 is missing from the version sequence because it was a project that was never officially released. The PHP team was working on a version 6 that was supposed to include native Unicode support. However, due to technical difficulties and other issues, the project was eventually abandoned. The next version released after PHP5 was PHP7.

How does the COMEFROM statement work in other programming languages?

The COMEFROM statement is not exclusive to PHP6. It’s also found in other programming languages like INTERCAL. However, its implementation and usage can vary. In some languages, it’s used as a debugging tool, while in others, it’s used to control program flow in a non-linear fashion.

What is the practical use of the COMEFROM statement?

The practical use of the COMEFROM statement is a topic of debate among developers. Some argue that it can make code more readable by clearly indicating where control is coming from. Others believe it can lead to confusing and hard-to-maintain code. It’s generally considered more of a novelty than a practical tool.

Why was the COMEFROM statement introduced in PHP6?

The COMEFROM statement was introduced in PHP6 as a humorous and unconventional approach to programming. It was not intended for practical use, but rather as a fun and interesting feature for developers to experiment with.

What happened to the PHP6 project?

The PHP6 project was abandoned due to technical difficulties and other issues. The main goal of the project was to introduce native Unicode support, but this proved to be a complex and challenging task. The PHP team decided to move on to PHP7, which includes many of the features originally planned for PHP6.

What is the difference between the GOTO and COMEFROM statements?

The GOTO statement directs the program flow to a specific point, while the COMEFROM statement specifies where the control should come from. They are essentially opposites. GOTO is widely used in many programming languages, while COMEFROM is less common and often considered a novelty.

Can I use the COMEFROM statement in current versions of PHP?

No, the COMEFROM statement was only introduced in PHP6, which was never officially released. It is not available in current versions of PHP.

What is the impact of the COMEFROM statement on code readability?

The impact of the COMEFROM statement on code readability is subjective. Some developers believe it can make code more readable by clearly indicating where control is coming from. Others argue that it can lead to confusing and hard-to-maintain code.

Are there any similar features to COMEFROM in current versions of PHP?

There are no features exactly like COMEFROM in current versions of PHP. However, PHP does have other control flow statements like GOTO, which can be used to direct program flow in a non-linear fashion.

Harry FuecksHarry Fuecks
View Author

Harry Fuecks is the Engineering Project Lead at Tamedia and formerly the Head of Engineering at Squirro. He is a data-driven facilitator, leader, coach and specializes in line management, hiring software engineers, analytics, mobile, and marketing. Harry also enjoys writing and you can read his articles on SitePoint and Medium.

Read Next
Get the freshest news and resources for developers, designers and digital creators in your inbox each week