progateのPHP学習コース3をやっています。
課題内容は料理注文サイトを作ることを目指したもので、今やっているのは商品一覧にあるCURRY,PASTAに、それぞれの辛さの数だけ唐辛子を表示するというものです。
質問内容は、なぜこのコードでokなのかが理解できない部分があるので、理由を教えて下さい。
詳細
index.phpにあるelse文の中で、for文を用いて、spicinessプロパティの数だけ繰り返し処理を行っているコードの部分なんですが、$i<=$menu->getSpiciness()のgetSpicinessメソッドがなぜ使えるのかに理解ができません。
自分ではrequire_onceで読み込んでいるのはdata.phpとmenu.phpだけで、getSpicinessメソッドを使うにはそれを定義しているfood.phpも読み込まないといけないのでは?と考えています。実際food.phpを読み込んでなくてもページに反映されています。
調べたこと
require_onceをprogate で解説しているのを読むと『require_onceで読み込んだファイルで定義されているクラスや変数を、require_onceを記述したファイル内で使うことができます。』とあります。メソッドは関係ないのかが調べても納得いく答えが見つけられませんでした。リンク内容
下コードindex.php
html
1コード 2<?php 3require_once('data.php'); 4require_once('menu.php'); 5?> 6 7<!DOCTYPE html> 8<html> 9<head> 10 <meta charset="utf-8"> 11 <title>Café Progate</title> 12 <link rel="stylesheet" type="text/css" href="stylesheet.css"> 13 <link href='https://fonts.googleapis.com/css?family=Pacifico|Lato' rel='stylesheet' type='text/css'> 14</head> 15<body> 16 <div class="menu-wrapper container"> 17 <h1 class="logo">Café Progate</h1> 18 <h3>メニュー<?php echo Menu::getCount() ?>品</h3> 19 <form method="post" action="confirm.php"> 20 <div class="menu-items"> 21 <?php foreach ($menus as $menu): ?> 22 <div class="menu-item"> 23 <img src="<?php echo $menu->getImage() ?>" class="menu-item-image"> 24 <h3 class="menu-item-name"><?php echo $menu->getName() ?></h3> 25 <?php if ($menu instanceof Drink): ?> 26 <p class="menu-item-type"><?php echo $menu->getType() ?></p> 27 <?php else: ?> 28 <!-- for文を用いて、spicinessプロパティの数だけ繰り返し処理を行ってください --> 29 <?php for($i=1;$i<=$menu->getSpiciness();$i++): ?> 30 <img class="icon-spiciness" src="https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/chilli.png"> 31 <?php endfor ?> 32 <?php endif ?> 33 <p class="price">¥<?php echo $menu->getTaxIncludedPrice() ?>(税込)</p> 34 <input type="text" value="0" name="<?php echo $menu->getName() ?>"> 35 <span>個</span> 36 </div> 37 <?php endforeach ?> 38 </div> 39 <input type="submit" value="注文する"> 40 </form> 41 </div> 42</body> 43</html> 44
下コード data.php
php
1コード 2<?php 3require_once('drink.php'); 4require_once('food.php'); 5 6$juice = new Drink('JUICE', 600, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/juice.png', 'アイス'); 7$coffee = new Drink('COFFEE', 500, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/coffee.png', 'ホット'); 8$curry = new Food('CURRY', 900, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/curry.png', 3); 9$pasta = new Food('PASTA', 1200, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/pasta.png', 1); 10 11$menus = array($juice, $coffee, $curry, $pasta); 12 13?>
下コードmenu.php
php
1コード 2<?php 3class Menu { 4 protected $name; 5 protected $price; 6 protected $image; 7 private $orderCount = 0; 8 protected static $count = 0; 9 10 public function __construct($name, $price, $image) { 11 $this->name = $name; 12 $this->price = $price; 13 $this->image = $image; 14 self::$count++; 15 } 16 17 public function hello() { 18 echo '私は'.$this->name.'です'; 19 } 20 21 public function getName() { 22 return $this->name; 23 } 24 25 public function getImage() { 26 return $this->image; 27 } 28 29 public function getOrderCount() { 30 return $this->orderCount; 31 } 32 33 public function setOrderCount($orderCount) { 34 $this->orderCount = $orderCount; 35 } 36 37 public function getTaxIncludedPrice() { 38 return floor($this->price * 1.08); 39 } 40 41 public function getTotalPrice() { 42 return $this->getTaxIncludedPrice() * $this->orderCount; 43 } 44 45 public static function getCount() { 46 return self::$count; 47 } 48 49} 50?>
下コード food.php
php
1コード 2<?php 3require_once('menu.php'); 4 5class Food extends Menu { 6 private $spiciness; 7 8 public function __construct($name, $price, $image, $spiciness) { 9 parent::__construct($name, $price, $image); 10 $this->spiciness = $spiciness; 11 } 12 13 public function getSpiciness() { 14 return $this->spiciness; 15 } 16 17} 18 19?>
追記
『data.php の冒頭で require_once('food.php');と読み込んでいますよ。』(kei344さん)の回答から、
直接require_onceでファイルの読み込みをしなくても良いんだな、間接的に読み込んでいる形でも良いんだなと理解した。
『require_onceで読み込んでいるところを読み込んでいるファイルのコードにそのまま置き換えると、どういう関係になっているかイメージがつくのではと思います。』(m.ts10806さん)の回答から、
ファイルを読み込むということを視覚的に理解しやすく、(kei344さん)の回答とあわせて読むと理解につながった。
『require_onceではメソッドも読み込まれます。』(mikkameさん)の回答から、
曖昧な理解を解消できたので助かりました。
ありがとうございました。
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/11 08:13
2019/08/11 09:28