LoginSignup
0
1

More than 3 years have passed since last update.

PHPのOOPについて自習したことをまとめてみる①(クラスの定義について)

Posted at

参考にしたサイト

クラスの定義

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というんですかね)

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1