Slide 17
Slide 17 text
例②
class Greet
{
function reply1(string $your_greet): string
{
$is_question = preg_match('/[\?]/', substr(trim($your_greet), -1));
$is_exclamation = preg_match('/[\!]/', substr(trim($your_greet), -1));
$is_space = preg_match('/^[\s]+$/', $your_greet);
// 末尾が?の場合
if ($is_question) {
$res = 'Wait a moment.';
// 末尾が!の場合
} elseif ($is_exclamation) {
$res = 'You\'re looking well';
// 文字が存在しない場合
} elseif (!$your_greet || $is_space) {
$res = 'Pardon?';
} else {
$res = 'Hello.';
}
return $res;
}
}
$greeting = New Greet;
var_dump($greeting->reply1('Hi!'));
- 渡された文字列の種類によって異なる返事を
するメソッド
- 判定用のパラメータを作ってから、if文で処理
を分けている