PHP 8.5.0 Alpha 1 available for testing

Voting

: max(zero, seven)?
(Example: nine)

The Note You're Voting On

metamarkers at gmail dot com
12 years ago
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);
}

}
?>

<< Back to user notes page

To Top