Skip to content

Instantly share code, notes, and snippets.

@uzulla
Created April 1, 2014 12:55
Show Gist options
  • Save uzulla/9913394 to your computer and use it in GitHub Desktop.
Save uzulla/9913394 to your computer and use it in GitHub Desktop.
PHPで、いわゆるPerlのGuard(ブロックを抜けたら実行するコードを前もって動的に設定する)をやる。 see also http://search.cpan.org/~mlehmann/Guard-1.022/Guard.pm
<?php
class Guard{
public $destruct;
public function __construct($callable){
$this->destruct = $callable;
}
public function __destruct(){
call_user_func($this->destruct);
}
}
chdir('/');
echo getcwd().PHP_EOL;
call_user_func(function(){
$g = new Guard(function(){
chdir('/');
});
chdir('/tmp');
echo getcwd().PHP_EOL;
});
echo getcwd().PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment