filePath = $filePath; } function lock($wait=10) { $this->filePointer = fopen($this->filePath,"w"); $lock = false; for($i = 0; $i < $wait && !($lock = flock($this->filePointer,LOCK_EX|LOCK_NB)); $i++) { sleep(1); } if(!$lock) { trigger_error("Not able to create a lock in $wait seconds"); } return $this->filePointer; } function unlock(): bool { $result = flock($this->filePointer,LOCK_UN); fclose($this->filePointer); @unlink($this->filePath); return $result; } }