Skip to content

Instantly share code, notes, and snippets.

@katsube
Last active November 21, 2022 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save katsube/92ebd3b7a900aeb6e77cc1cbb5d0cf18 to your computer and use it in GitHub Desktop.
Save katsube/92ebd3b7a900aeb6e77cc1cbb5d0cf18 to your computer and use it in GitHub Desktop.
<?php
$tarou = new User('太郎');
$tarou->addExp(300);
// 現在のレベルを表示 ("太郎 Lv.4"と表示される)
printf("%s Lv.%d\n"
, $tarou->name
, $tarou->getLevel());
/**
* ユーザークラス
*/
class User{
public $name; // 名前 ※外から参照可能
private $exp; // 経験値 ※外から参照不可能
/**
* コンストラクタ
*/
function __construct($name, $exp=1){
$this->name = $name;
$this->exp = $exp;
}
/**
* 経験値を加算する
*/
function addExp($value){
$this->exp += $value;
}
/**
* レベルを計算し返却する
*/
// ★このあたりにメソッドを追加する★
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment