質問するログイン新規登録

質問編集履歴

3

menu.phpの追加

2019/08/25 05:23

投稿

myksgo
myksgo

スコア21

title CHANGED
File without changes
body CHANGED
@@ -104,6 +104,58 @@
104
104
 
105
105
  ?>
106
106
  ```
107
+ ```php
108
+ //menu.php
109
+ <?php
110
+ class Menu {
111
+ protected $name;
112
+ protected $price;
113
+ protected $image;
114
+ private $orderCount = 0;
115
+ protected static $count = 0;
116
+
117
+ public function __construct($name, $price, $image) {
118
+ $this->name = $name;
119
+ $this->price = $price;
120
+ $this->image = $image;
121
+ self::$count++;
122
+ }
123
+
124
+ public function hello() {
125
+ echo '私は'.$this->name.'です';
126
+ }
127
+
128
+ public function getName() {
129
+ return $this->name;
130
+ }
131
+
132
+ public function getImage() {
133
+ return $this->image;
134
+ }
135
+
136
+ public function getOrderCount() {
137
+ return $this->orderCount;
138
+ }
139
+
140
+ public function setOrderCount($orderCount) {
141
+ $this->orderCount = $orderCount;
142
+ }
143
+
144
+ public function getTaxIncludedPrice() {
145
+ return floor($this->price * 1.08);
146
+ }
147
+
148
+ public function getTotalPrice() {
149
+ return $this->getTaxIncludedPrice() * $this->orderCount;
150
+ }
151
+
152
+ public static function getCount() {
153
+ return self::$count;
154
+ }
155
+
156
+ }
157
+ ?>
158
+ ```
107
159
  setとgetについてよく分からなくなってしまいましたので、ご教授よろしくお願いいたします。
108
160
 
109
161
  現在progateでPHPの勉強をしているのですが、上記のコードのgetType()+setType()を指定するパターンと

2

data.phpの追加

2019/08/25 05:23

投稿

myksgo
myksgo

スコア21

title CHANGED
File without changes
body CHANGED
@@ -89,6 +89,21 @@
89
89
  </html>
90
90
 
91
91
  ```
92
+ ```php
93
+ //data.php
94
+ <?php
95
+ require_once('drink.php');
96
+ require_once('food.php');
97
+
98
+ $juice = new Drink('JUICE', 600, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/juice.png', 'アイス');
99
+ $coffee = new Drink('COFFEE', 500, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/coffee.png', 'ホット');
100
+ $curry = new Food('CURRY', 900, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/curry.png', 3);
101
+ $pasta = new Food('PASTA', 1200, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/pasta.png', 1);
102
+
103
+ $menus = array($juice, $coffee, $curry, $pasta);
104
+
105
+ ?>
106
+ ```
92
107
  setとgetについてよく分からなくなってしまいましたので、ご教授よろしくお願いいたします。
93
108
 
94
109
  現在progateでPHPの勉強をしているのですが、上記のコードのgetType()+setType()を指定するパターンと

1

index.phpの追加。

2019/08/25 05:22

投稿

myksgo
myksgo

スコア21

title CHANGED
File without changes
body CHANGED
@@ -1,4 +1,5 @@
1
1
  ```PHP
2
+ //drink.php
2
3
  <?php
3
4
  require_once('menu.php');
4
5
 
@@ -23,6 +24,7 @@
23
24
  ?>
24
25
  ```
25
26
  ```PHP
27
+ //food.php
26
28
  <?php
27
29
  require_once('menu.php');
28
30
 
@@ -42,6 +44,51 @@
42
44
 
43
45
  ?>
44
46
  ```
47
+ ```php
48
+ //index.php
49
+ <?php
50
+ require_once('data.php');
51
+ require_once('menu.php');
52
+ ?>
53
+
54
+ <!DOCTYPE html>
55
+ <html>
56
+ <head>
57
+ <meta charset="utf-8">
58
+ <title>Café Progate</title>
59
+ <link rel="stylesheet" type="text/css" href="stylesheet.css">
60
+ <link href='https://fonts.googleapis.com/css?family=Pacifico|Lato' rel='stylesheet' type='text/css'>
61
+ </head>
62
+ <body>
63
+ <div class="menu-wrapper container">
64
+ <h1 class="logo">Café Progate</h1>
65
+ <h3>メニュー<?php echo Menu::getCount() ?>品</h3>
66
+ <form method="post" action="confirm.php">
67
+ <div class="menu-items">
68
+ <?php foreach ($menus as $menu): ?>
69
+ <div class="menu-item">
70
+ <img src="<?php echo $menu->getImage() ?>" class="menu-item-image">
71
+ <h3 class="menu-item-name"><?php echo $menu->getName() ?></h3>
72
+ <?php if ($menu instanceof Drink): ?>
73
+ <p class="menu-item-type"><?php echo $menu->getType() ?></p>
74
+ <?php else: ?>
75
+ <?php for($i=0;$i<$menu->getSpiciness();$i++):?>
76
+ <img class="icon-spiciness" src="https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/chilli.png">
77
+ <?php endfor ?>
78
+ <?php endif ?>
79
+ <p class="price">¥<?php echo $menu->getTaxIncludedPrice() ?>(税込)</p>
80
+ <input type="text" value="0" name="<?php echo $menu->getName() ?>">
81
+ <span>個</span>
82
+ </div>
83
+ <?php endforeach ?>
84
+ </div>
85
+ <input type="submit" value="注文する">
86
+ </form>
87
+ </div>
88
+ </body>
89
+ </html>
90
+
91
+ ```
45
92
  setとgetについてよく分からなくなってしまいましたので、ご教授よろしくお願いいたします。
46
93
 
47
94
  現在progateでPHPの勉強をしているのですが、上記のコードのgetType()+setType()を指定するパターンと
@@ -49,4 +96,9 @@
49
96
 
50
97
  どういったときに、setとgetが必要なのか、またgetのみの指定でよいのかご教授お願いいたします。
51
98
 
52
- どうやって説明したら良いのか分からずわかりにくい質問になり申し訳ございません。
99
+ どうやって説明したら良いのか分からずわかりにくい質問になり申し訳ございません。
100
+
101
+ 追記
102
+ 「ホット」や「アイス」を指定するときにget+setで定義し、「辛さ」を表すspicinessではgetのみの定義となっております。
103
+ 例えばフォームなどの入力してもらう項目に対してはsetを使い項目をsetしその項目をgetで取得しているのかなと思っておりましたが、違うようで混乱しております。
104
+ よろしくお願いいたします。