色々
- インスタンスの生成はクラスの外
- インスタンス生成は $変数名 = new クラス名();
- クラス(設計図)にはそのインスタンス(実体)が持つ「プロパティ」と「メソッド」の2つを定義
- プロパティとはインスタンスが持つデータのことで、メソッドはインスタンスに関連する処理(関数)のこと
- なんか、ジャヴァと似てますね
- プロパティは「public $プロパティ名」
- インスタンス->プロパティ名」とすることで、そのインスタンスのプロパティにアクセスすることができる(プロパティ名に$は不要)
- メソッドは「public function メソッド名()」
- ↑なんか、言語によってfunctionがいったりいらんかったり、ややこいね。
- メソッド呼び出し例 $curry->hello();
- thisについて(↓に別項目にした)
- コンストラクタもある 例 public function __construct(){
- コンストルクト
- PHPのコードを1行で書き表す場合は、区切る必要がないのでセミコロンを省略することができる
- foreach文(if,for,while,switch等も)をHTMLに埋め込む(↓参照)
- 読み込み 例 require_once('menu.php');
- ジャヴァ同様、カプセル化もある。private。
- なので当然、ゲッターやセッターを使う
thisとは(また出たよ・・)
- メソッド内でインスタンスのプロパティやメソッドにアクセスしたい時には「$this」という特殊変数。
- $thisはクラス内のメソッドの定義の中でのみ使用できる
- $thisはメソッドが呼ばれた時に、そのメソッドを呼び出しているインスタンスに置き換えられる
foreach文をHTMLに埋め込む
- foreach文の「{」の代わりに「:」
- 「}」の代わりに「endforeach」と記述
- その間に処理を書いていく。この処理部分にはHTMLのタグを書くことができるので便利。
- if文、for文、while文やswitch文などもforeach文と同様に、それぞれ「endif」、「endfor」、「endwhile」、「endswitch」を使って書くことができる
総合演習
<?php require_once('data.php') ?>
<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>
<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>
<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
class Menu {
private $name;
private $price;
private $image;
private $orderCount = 0;
public function __construct($name, $price, $image) {
$this->name = $name;
$this->price = $price;
$this->image = $image;
}
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;
}
}
?>
<?php
require_once('menu.php');
$juice = new Menu('JUICE', 600, 'https://~~.png');
$coffee = new Menu('COFFEE', 500, 'https://~~.png');
$curry = new Menu('CURRY', 900, 'https://~~.png');
$pasta = new Menu('PASTA', 1200, 'https://~~.png');
$menus = array($juice, $coffee, $curry, $pasta);
?>
<?php require_once('data.php') ?>
<html>
<head>
<meta charset="utf-8">
<title>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="order-wrapper">
<h2>注文内容確認</h2>
<?php $totalPayment=0 ?>
<?php foreach ($menus as $menu): ?>
<?php
$orderCount = $_POST[$menu->getName()];
$menu->setOrderCount($orderCount);
$totalPayment+=$menu->getTotalPrice();
?>
<p class="order-amount">
<?php echo $menu->getName() ?>
x
<?php echo $orderCount ?>
個
</p>
<p class="order-price"><?php echo $menu->getTotalPrice() ?>円</p>
<?php endforeach ?>
<h3>合計金額: <?php echo $totalPayment ?>円</h3>
</div>
</body>
</html>