Skip to content

Instantly share code, notes, and snippets.

@matslindh
Created December 15, 2017 16:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matslindh/3ae56a8bd1d305e681a9ec4664587823 to your computer and use it in GitHub Desktop.
Save matslindh/3ae56a8bd1d305e681a9ec4664587823 to your computer and use it in GitHub Desktop.
<?php
/**
The MIT License (MIT)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
class ReturnException extends Exception
{
protected $value = null;
public function __construct($value)
{
$this->value = $value;
}
public function getValue()
{
return $this->value;
}
}
class LoopFinishedException extends Exception { }
class TrueException extends Exception { }
class FalseException extends Exception { }
class IfceptionException extends Exception { }
function ExceptionLooper($count, $func)
{
$value = null;
try
{
$func();
}
catch (ReturnException $return)
{
$value = $return;
}
try
{
ifception($count, 0);
}
catch (FalseException $false)
{
ExceptionLooper(--$count, $func);
}
catch (TrueException $true)
{
throw new LoopFinishedException("Loop finished", 0, $value);
}
ExceptionLooper(--$count, $func);
}
function ifception($value, $expected)
{
$expectedException = 'Ifception' . $expected;
$actualException = 'Ifception' . $value;
class_alias('IfceptionException', $actualException);
eval("
try
{
throw new \$actualException;
}
catch ($expectedException \$true)
{
throw new TrueException();
}
catch ($actualException \$false)
{
throw new FalseException();
}
");
}
try
{
ExceptionLooper(10, function () {
static $a = 0;
static $b = 1;
$c = $a + $b;
$a = $b;
$b = $c;
printf($c . " ");
throw new ReturnException($c);
});
}
catch (LoopFinishedException $loop)
{
print("<br />" . $loop->getPrevious()->getValue() . "<br />");
}
Copy link

ghost commented Dec 16, 2017

This is offensive to basic programming sensibilities.

I love it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment