1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【PHP】クラスとは

Last updated at Posted at 2022-03-04

※クラスをなぜ使うか?

共通で使いたい処理をまとめていつでも呼び出せるようにしたいから

<?php

class Food
{
    public $cat = "キャットフード";
    public $dog = "ドッグフード";
    public $bird = "パフ・ザ・フルーツ";
    
    public function cry($animal) {
        if($animal === 'cat') return $this->cat;
        if($animal === 'dog') return $this->dog;
        if($animal === 'bird') return $this->bird;
    }
}

$food = new Food();

echo "猫の餌:".$food->cry('cat');
echo PHP_EOL;
echo "犬の餌:".$food->cry('dog');
echo PHP_EOL;
echo "鳥の餌:".$food->cry('bird');

結果

猫の餌:キャットフード
犬の餌:ドッグフード
鳥の餌:パフ・ザ・フルーツ
1
0
4

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?