you can wrap a lock as an object to make it a scope-based lock. when the lock object is no longer referenced, like when it's unset or the owner returns, the destructor will call unlock.
this way you can just create a lock object and forget about it.
<?php
class lock {
private $handle;
public static function read ( $handle ) {
$lock = new static();
$lock->handle = $handle;
return flock($handle,LOCK_SH) ? $lock : false;
}
public static function write ( $handle ) {
$lock = new static();
$lock->handle = $handle;
return flock($handle,LOCK_EX) ? $lock : false;
}
public function __destruct ( ) {
flock($this->handle,LOCK_UN);
}
}
?>