参考にしたサイト
クラスの定義
makeClass.php
<?php
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
?>
・クラスとは→プロパティ(変数)やメソッド(関数)を集めたまとまりのこと。
クラスの定義2
makeClass.php
<?php
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
function set_color($color) {
$this->color = $color;
}
function get_color() {
return $this->color;
}
}
$apple = new Fruit();
$apple->set_name('Apple');
$apple->set_color('Red');
echo "Name: " . $apple->get_name();
echo "<br>";
echo "Color: " . $apple->get_color();
?>
・メソッドを通じてプロパティにアクセスすると、こんな感じ。(getter, setterというんですかね)