回答編集履歴

2

chousei

2022/10/20 01:52

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -1,5 +1,6 @@
1
1
  たとえばユーザーのステータス管理ならこんな感じ
2
+ ※調整しました
2
- ````PHP
3
+ ```PHP
3
4
  <?PHP
4
5
  class Player{
5
6
  private $turn=0;
@@ -32,13 +33,13 @@
32
33
  $this->log=$log;
33
34
  }
34
35
  public function increment_turn(){
35
- $this->turn++;
36
+ $this->set_turn($this->get_turn()+1);
36
37
  }
37
38
  public function add_gold($gold){
38
- $this->gold+=$gold;
39
+ $this->set_gold($this->get_gold()+$gold);
39
40
  }
40
41
  public function append_log($island){
41
- array_push($this->log,$island);
42
+ $this->set_log(array_merge($this->log,[$island]));
42
43
  }
43
44
  }
44
45
  $status=["turn"=>3,"gold"=>200,"log"=>['A','B']];
@@ -49,3 +50,4 @@
49
50
  $status=$player->get_status();
50
51
  var_dump($status);
51
52
  ```
53
+

1

調整

2022/10/20 01:39

投稿

yambejp
yambejp

スコア114883

test CHANGED
@@ -10,25 +10,25 @@
10
10
  $this->set_gold($status["gold"]??0);
11
11
  $this->set_log($status["log"]??[]);
12
12
  }
13
- public function get_turn(){
13
+ private function get_turn(){
14
14
  return $this->turn;
15
15
  }
16
- public function get_gold(){
16
+ private function get_gold(){
17
17
  return $this->gold;
18
18
  }
19
- public function get_log(){
19
+ private function get_log(){
20
20
  return $this->log;
21
21
  }
22
22
  public function get_status(){
23
23
  return ["turn"=>$this->get_turn(),"gold"=>$this->get_gold(),"log"=>$this->get_log()];
24
24
  }
25
- public function set_turn($turn){
25
+ private function set_turn($turn){
26
26
  $this->turn=$turn;
27
27
  }
28
- public function set_gold($gold){
28
+ private function set_gold($gold){
29
29
  $this->gold=$gold;
30
30
  }
31
- public function set_log($log){
31
+ private function set_log($log){
32
32
  $this->log=$log;
33
33
  }
34
34
  public function increment_turn(){
@@ -41,7 +41,7 @@
41
41
  array_push($this->log,$island);
42
42
  }
43
43
  }
44
- $status=["turn"=>3,"gold"=>200,"log"=>['A','B']]; //全ページからの引き継ぎ
44
+ $status=["turn"=>3,"gold"=>200,"log"=>['A','B']];
45
45
  $player=new Player($status);
46
46
  $player->increment_turn();
47
47
  $player->add_gold(100);