progateのPHP学習コース2をやっています。
今やっている課題は、料理注文サイトを作ることを目指しています。
注文したJUICE、COFFEE、CURRY、PASTAの4つの商品の個数と金額をだす、つまり今行なっている作業はformの中でそれらの情報を送信して、値を受け取り小計金額をだす作業です。
今回、理解できない部分のコードがあるので そのコードがなぜいるのかを教えて下さい。
詳細〜
上から2つ目のコードグループにある
php
1コード 2public function getTaxIncludedPrice() { 3 return floor($this->price * 1.08); 4 } 5// getTotalPriceメソッドを定義してください 6 public function getTotalPrice(){ 7 return $this->getTaxIncludedPrice()*$this->orderCount; 8 }
ですが、これは正解のコードです。
自分はこのコードに到るまでにreturn getTaxIncludedPrice()*$this->orderCount;としていました。
public function getTaxIncludedPrice() {
の中に$this->priceが入っているから、改めて$thisー>を getTaxIncludedPrice()の頭に入れなくてもいいんでは?と考えています。書かないでやってみるとクリアできませんでした。かくべき理由が分からないので教えて下さい。
html
1コード 2<?php require_once('data.php') ?> 3 4<!DOCTYPE html> 5<html> 6<head> 7 <meta charset="utf-8"> 8 <title>Café Progate</title> 9 <link rel="stylesheet" type="text/css" href="stylesheet.css"> 10 <link href='https://fonts.googleapis.com/css?family=Pacifico|Lato' rel='stylesheet' type='text/css'> 11</head> 12<body> 13 <div class="menu-wrapper container"> 14 <h1 class="logo">Café Progate</h1> 15 <form method="post" action="confirm.php"> 16 <div class="menu-items"> 17 <?php foreach ($menus as $menu): ?> 18 <div class="menu-item"> 19 <img src="<?php echo $menu->getImage() ?>" class="menu-item-image"> 20 <h3 class="menu-item-name"><?php echo $menu->getName() ?></h3> 21 <p class="price">¥<?php echo $menu->getTaxIncludedPrice() ?>(税込)</p> 22 <input type="text" value="0" name="<?php echo $menu->getName() ?>"> 23 <span>個</span> 24 </div> 25 <?php endforeach ?> 26 </div> 27 <input type="submit" value="注文する"> 28 </form> 29 </div> 30</body> 31</html> 32
php
1コード 2<?php 3class Menu { 4 private $name; 5 private $price; 6 private $image; 7 private $orderCount = 0; 8 9 public function __construct($name, $price, $image) { 10 $this->name = $name; 11 $this->price = $price; 12 $this->image = $image; 13 } 14 15 public function hello() { 16 echo '私は'.$this->name.'です'; 17 } 18 19 public function getName() { 20 return $this->name; 21 } 22 23 public function getImage() { 24 return $this->image; 25 } 26 27 public function getOrderCount() { 28 return $this->orderCount; 29 } 30 31 public function setOrderCount($orderCount) { 32 $this->orderCount = $orderCount; 33 } 34 35 public function getTaxIncludedPrice() { 36 return floor($this->price * 1.08); 37 } 38 39 // getTotalPriceメソッドを定義してください 40 public function getTotalPrice(){ 41 return $this->getTaxIncludedPrice()*$this->orderCount; 42 } 43 44} 45?>
php
1コード 2<?php 3require_once('menu.php'); 4 5$juice = new Menu('JUICE', 600, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/juice.png'); 6$coffee = new Menu('COFFEE', 500, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/coffee.png'); 7$curry = new Menu('CURRY', 900, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/curry.png'); 8$pasta = new Menu('PASTA', 1200, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/pasta.png'); 9 10$menus = array($juice, $coffee, $curry, $pasta); 11 12?>
php
1コード 2<?php require_once('data.php') ?> 3 4<!DOCTYPE html> 5<html> 6<head> 7 <meta charset="utf-8"> 8 <title>Progate</title> 9 <link rel="stylesheet" type="text/css" href="stylesheet.css"> 10 <link href='https://fonts.googleapis.com/css?family=Pacifico|Lato' rel='stylesheet' type='text/css'> 11</head> 12<body> 13 <div class="order-wrapper"> 14 <h2>注文内容確認</h2> 15 <?php foreach ($menus as $menu): ?> 16 <?php 17 $orderCount = $_POST[$menu->getName()]; 18 // $menuに対して、$orderCountを引数としてsetOrderCountメソッドを呼び出してください 19 $menu->setOrderCount($orderCount); 20 21 ?> 22 <p class="order-amount"> 23 <?php echo $menu->getName() ?> 24 x 25 <?php echo $orderCount ?> 26 個 27 </p> 28 <!-- $menuに対してgetTotalPriceメソッドを呼び出して、金額を表示してください --> 29 <p class="order-price"><?php echo $menu->getTotalPrice() ?>円</p> 30 <?php endforeach ?> 31 </div> 32</body> 33</html>
追記
『クラスが持つ情報にアクセスする際には必ず「所属」を明示する必要があります』
『メソッドを呼ぶ場合は、常に$obj->method()の形式で書く必要があります。』
上記2つから、必ず、常に所属クラスを明示する必要があるんだなと分かった。
『所属クラスが明示されていない場合は、既に回答があるように何のクラスにも属していない関数を実行します。』
勉強になった。
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/13 05:52