-1

I am writing some kind of code analyser for PHP, and I am searching for real-life examples of use of "goto" instruction in frameworks / scripts.

So far I've found only some marginal usage in Symfony testing suite. It seems that this language feature is not used at all in userland? Or it exists somewhere?

ts.
  • 10,510
  • 7
  • 47
  • 73

1 Answers1

0

New answer

Recently, I began work at a new employer which had inherited a legacy [bespoke] PHP partial OO and partial MCV framwork. On some of the pages, the views were performing a good amount of business logic without actually displaying anything; each time, these flat PHP files were included (rather than include_once()) and so what was happening is that many parts of the site were spaghetti and meat balls type scripting (something which GOTO is supposed to produce so is considered bad practise).

What was happening was that some logic was firing (because it was included multiple times and in multiple places) when it didn't need to be, and creating error logs because maybe one line or one IF statement block was required in the whole file of hundreds of lines of PHP.

Creating an end-point and adding in goto END; was a good interim step to reducing the errors logs and from stopping those bits of script from firing when they didn't need to. For instance:

if(empty($userRequest)){
    goto END;
}
## The rest of the script from this point needs to fire until...

if(empty($_REQUEST['user_id'])){
    goto END;
}
## etc...

Of course, this is only an interim step to actually reduce the amount of spaghetti. I've also moved some necessary functionality to controller classes where it was practical to do so (and created models where the supposed view files were doing the data modelling).

I could have added in IF...ELSE statement blocks to achieve the same results, but the amount of logic in the script (and the amount of nested IF statements etc...) meant that adding in goto END; actually made the script more readable whilst other refatoring was happening.

Eventually, when I've untangled all of the legacy spaghetti, I'll remove the GOTO statements and make it fully MVC and OO as it should be.

Old answer

Here is an example of using goto:

<?php
    ten:
    print "Hello ";

    goto ten;

You are right, it is almost never used in PHP programming because modern programming wisdom says that it leads to "spaghetti code" which is more difficult to debug.

However, try not using non-conditional loops (which is what goto actually is) in machine-level languages. I remember using non-conditional loops with MIPs assembly a few years back.

Shaun Bebbers
  • 179
  • 2
  • 12
  • 1
    What I need is a reallife example of real use in framework / script, not manual / rfc / test example – ts. Apr 26 '17 at 13:32
  • In non-OO scripts, you can replace the `function name()` with simply `name:` if the function does not require parameters and is not expected to return a value. That is about as real as I can think of. Obviously, you'll have to `break;` or something, or one part of your script will continue to the next. – Shaun Bebbers Apr 26 '17 at 14:48
  • I'm going to convert some of my PHP scripts to use `GOTO` and I'll let you know how I get on. – Shaun Bebbers May 08 '17 at 15:14
  • @ts. - I've used the `goto` command in my PHP framework, for instance, here: https://github.com/sbebbers/FrameWork.php/blob/master/application/controller/DateController.php#L68 - Of course, there is probably a better way to achieve what I'm doing within the controller but I thought that I'd give it a go. – Shaun Bebbers Nov 15 '17 at 09:01