Created
April 1, 2014 12:55
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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