class FTP implements Disposable {
private $stream;
private function __construct() {
$this->stream = ftp_connect();
}
public static function using(callable $consumer) {
$instance = new FTP();
try {
call_user_func($consumer, $instance);
} finally {
$instance->__dispose();
}
}
public function __dispose() {
ftp_close($this->stream);
}
}
FTP::using(function($ftp) {
// use here, will be cleaned up
});