BNBのプログラミング勉強記録

ガチのプログラミング初心者が駆け上がっていくブログ

メモ(php III)

基本

  • インスタンスの数を把握するには?→インスタンスの数は個々のインスタンスではなく、クラス全体で管理する必要がある→「クラスプロパティ」というものを用いるとこれを実現できる
  • 個々のインスタンスがもつデータはプロパティ
  • クラスがもつデータはクラスプロパティ。staticを付ける。
  • クラスプロパティにアクセスする場合は「クラス名::$クラスプロパティ名」
  • クラス内でクラスプロパティにアクセスする際は「self」
  • selfは、クラスの中で使うとそのクラス自身のことを指し示し、「self::$クラスプロパティ名」のように使う
  • $countのゲッターのように、個々のインスタンスのデータに関係ない処理を行いたい時には「クラスメソッド」
  • クラスメソッドは「static」を用いて定義
  • ジャヴァ同様、継承もある。「class 子クラス名 extends 親クラス名」
  • 子クラスにメソッドが定義されている場合にはそのメソッドが、定義されていない場合には親クラスのメソッドが呼び出される(これもジャヴァと一緒ですね)
  • 演習やってて気づいたんだけど、俺、echo 忘れがち
  • 「instanceof」を用いると、あるインスタンスが特定のクラスのインスタンスであるかどうか判別することができる
  • インスタンス instanceof クラス名」のようにすると、インスタンスが指定したクラスのインスタンスである場合は「true」に、そうでない場合は「false」に
  • 同じ名前のメソッドを子クラスで定義するとメソッドの中身を上書きすることができる(オーバーライド)。ジャヴァと一緒。
  • 子クラスから親クラスで定義したプロパティにアクセスしたい場合は、そのプロパティのアクセス権を「protected」に。ジャヴァと一緒。
  • オーバーライドの際に親クラスで定義したメソッドを呼び出したいときには、「parent」を用いて「parent::メソッド名」のようにする
  • 「parent::メソッド名」を記述した場所で、親クラスのメソッドが実行される
  • if():~endifのように書けることの応用→elseを用いる場合はif():~else:~endifのように書く
  • ↑この書き方、なかなか慣れんわ・・

総合演習

<?php 
require_once('data.php');
require_once('menu.php');
?>

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Café Progate</title>
  <link rel="stylesheet" type="text/css" href="stylesheet.css">
  <link href='https://fonts.googleapis.com/css?family=Pacifico|Lato' rel='stylesheet' type='text/css'>
</head>
<body>
  <div class="menu-wrapper container">
    <h1 class="logo">Café Progate</h1>
    <h3>メニュー<?php echo Menu::getCount() ?></h3>
    <form method="post" action="confirm.php">
      <div class="menu-items">
        <?php foreach ($menus as $menu): ?>
          <div class="menu-item">
            <img src="<?php echo $menu->getImage() ?>" class="menu-item-image">
            <h3 class="menu-item-name"><?php echo $menu->getName() ?></h3>
            <?php if ($menu instanceof Drink): ?>
              <p class="menu-item-type"><?php echo $menu->getType() ?></p>
            <?php else: ?>

              <?php for($i=0;$i<$menu->getSpiciness();$i++): ?>
              <img src="https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/chilli.png" class="icon-spiciness">
              <?php endfor ?>
              
            <?php endif ?>
            <p class="price">¥<?php echo $menu->getTaxIncludedPrice() ?>(税込)</p>
            <input type="text" value="0" name="<?php echo $menu->getName() ?>">
            <span></span>
          </div>
        <?php endforeach ?>
      </div>
      <input type="submit" value="注文する">
    </form>
  </div>
</body>
</html>
<?php
require_once('drink.php');
require_once('food.php');

$juice = new Drink('JUICE', 600, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/juice.png', 'アイス');
$coffee = new Drink('COFFEE', 500, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/coffee.png', 'ホット');
$curry = new Food('CURRY', 900, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/curry.png', 3);
$pasta = new Food('PASTA', 1200, 'https://s3-ap-northeast-1.amazonaws.com/progate/shared/images/lesson/php/pasta.png', 1);

$menus = array($juice, $coffee, $curry, $pasta);

?>
<?php
class Menu {
  protected $name;
  protected $price;
  protected $image;
  private $orderCount = 0;
  protected static $count = 0;
  
  public function __construct($name, $price, $image) {
    $this->name = $name;
    $this->price = $price;
    $this->image = $image;
    self::$count++;
  }
  
  public function hello() {
    echo '私は'.$this->name.'です';
  }
  
  public function getName() {
    return $this->name;
  }
  
  public function getImage() {
    return $this->image;
  }
  
  public function getOrderCount() {
    return $this->orderCount;
  }
  
  public function setOrderCount($orderCount) {
    $this->orderCount = $orderCount;
  }
  
  public function getTaxIncludedPrice() {
    return floor($this->price * 1.08);
  }
  
  public function getTotalPrice() {
    return $this->getTaxIncludedPrice() * $this->orderCount;
  }
  
  public static function getCount() {
    return self::$count;
  }
  
}
?>
<?php 
require_once('menu.php');

class Drink extends Menu {
  private $type;
  
  public function __construct($name, $price, $image, $type) {
    parent::__construct($name, $price, $image);
    $this->type = $type;
  }
  
  public function getType() {
    return $this->type;
  }
  
  public function setType($type) {
    $this->type = $type;
  }
  
}

?>
<?php 
require_once('menu.php');

class Food extends Menu {
  private $spiciness;
  
  public function __construct($name, $price, $image, $spiciness) {
    parent::__construct($name, $price, $image);
    $this->spiciness = $spiciness;
  }
  
  public function getSpiciness() {
    return $this->spiciness;
  }
  
}

?>