Last active
November 22, 2022 16:43
-
-
Save katsube/21cb2208c81da0629707ed26ac380bc2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//------------------------------------ | |
// 定数 | |
//------------------------------------ | |
// 保存するファイル | |
define('FILE', 'data.txt'); | |
//------------------------------------ | |
// メイン処理 | |
//------------------------------------ | |
// 保存するデータ | |
$data = json_encode([ | |
'name' => 'Taro', | |
'score' => 100 | |
]); | |
// 保存する | |
$player1 = new PlayData(FILE); | |
$player1->save($data); | |
// 保存したデータを確認する | |
echo $player1->load(); // {"name":"Taro","score":100} と表示される | |
/** | |
* プレイデータを保存するクラス | |
*/ | |
class PlayData{ | |
// ★このクラスを完成させる★ | |
/** | |
* セーブデータを読み込む | |
*/ | |
function load(){ | |
$data = ''; | |
$fp = fopen($this->file, 'r'); | |
flock($fp, LOCK_SH); | |
while(($buff = fgets($fp)) !== false){ | |
$data .= $buff; | |
} | |
flock($fp, LOCK_UN); | |
fclose($fp); | |
return($data); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment