PHP基礎学習
Chapter5. Classとオブジェクト指向
Class(クラス)は変数と関数のセットです。 本章では、Classを使用したオブジェクト指向プログラミングについて学習します。
オブジェクト指向については、論理的な視点や言語仕様で解釈が異なりますが、 ここでは、PHPの実践的な視点で解説しています。
Class(クラス)は変数と関数のセットです。 本章では、Classを使用したオブジェクト指向プログラミングについて学習します。
オブジェクト指向については、論理的な視点や言語仕様で解釈が異なりますが、 ここでは、PHPの実践的な視点で解説しています。
class Order {
public $price;
public $count;
public $tax;
public function sum() {
return $this->price * $this->count;
}
public function amount() {
$unit = $this->price + ($this->price * $this->tax / 100);
return $unit * $this->count;
}
}
$order1 = new Order();
$order1->price = 1000;
$order1->count = 2;
$order1->tax = 10;
$order2 = new Order();
$order2->price = 500;
$order2->count = 1;
$order2->tax = 8;
$sum1 = $order1->sum();
var_dump($sum1);
$sum2 = $order2->sum();
var_dump($sum2);
$total1 = $order1->amount();
var_dump($total1);
$total2 = $order2->amount();
var_dump($total2);
class Order {
public $price;
public $count;
public $tax;
public function __constract($price, $count, $tax) {
$this->price = $price;
$this->count = $count;
$this->tax = $tax;
}
public function sum() {
return $this->price * $this->count;
}
public function amount() {
$unit = $this->price + ($this->price * $this->tax / 100);
return $unit * $this->count;
}
}
コンストラクタを定義した場合、下記のようにして初期データをコンストラクタに渡します。
こうすることで、作成されたインスタンスのプロパティにはデータが代入された状態になります。
$order1 = new Order(1000, 2, 10);
$order2 = new Order(500, 1, 8);
$sum1 = $order1->sum();
$sum2 = $order2->sum();
class Pager {
private $count;
private $limit;
private $pagemax;
private $page;
private $offset;
public function __set(string $name, $value) {
switch ($name) {
case "limit":
$this->limit = $value;
break;
case "count":
$max = (int) ($value / $this->limit);
if (($value % $this->limit) > 0) $max++;
if ($max == 0) $max = 1;
$this->count = $value;
$this->pagemax = $max;
break;
case "page":
$this->page = $value;
$this->offset = ($value - 1) * $this->limit;
break;
}
}
public function __get(string $name) {
return $this->$name;
}
}
$pager1 = new Pager();
$pager1->limit = 10;
$pager1->count = 100;
var_dump($pager1->pagemax);
$pager1->page = 2;
var_dump($pager1->offset);
class Page {
private $path;
protected $values;
protected $errors;
public function __constract() {
$this->path = $this->getPath();
$this->values = array();
$this->values = array();
}
public function location(string $url) {
header("Location: {$url}");
exit();
}
public function response($response, $succeed) {
$response["status"] = ($succeed) ? "SUCCEED" : "FAILURE";
$response["hasError"] = (!$succeed);
echo json_encode($response);
exit();
}
}
class Page_Index extends Page {
/**
* サインインイベント
*/
public function signin() {
$_SESSION["member.profile"]["email"] = $_POST["email"];
$_SESSION["member.signin"] = date("Y/m/d H:i:s");
$res["url"] = "/member/index.html";
$this->response($res, true);
}
}
class Filter {
public static function isExpire(string $date, int $time) {
$target = strtotime($date);
return ((time() - $target) > ($time * 60));
}
}
$expire = Filter::isExpire($_SESSION["member.signin"], 20);
var_dump($expire);