Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Counter{
- private $counterFileName;
- public function __construct($inFN){
- if(!file_exists($inFN)){
- $counterFilePointer = fopen($inFN, 'w');
- flock($counterFilePointer, LOCK_EX);
- fwrite($counterFilePointer, '0');
- flock($counterFilePointer, LOCK_UN);
- fclose($counterFilePointer);
- }
- $this->counterFileName = $inFN;
- }
- public function getCount(){
- if($counterFilePointer = fopen($this->counterFileName, 'r')){
- flock($counterFilePointer, LOCK_SH);
- $count = (int)fgets($counterFilePointer);
- flock($counterFilePointer, LOCK_UN);
- fclose($counterFilePointer);
- return $count;
- }else{
- throw new Exception("ファイルオープン失敗");
- }
- }
- public function incrementCount($val = 1){
- $count = $this->getCount() + $val;
- if($counterFilePointer = fopen($this->counterFileName, 'w')){
- flock($counterFilePointer, LOCK_EX);
- fwrite($counterFilePointer, $count);
- flock($counterFilePointer, LOCK_UN);
- fclose($counterFilePointer);
- return $count;
- }else{
- throw new Exception("ファイルオープン失敗");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement