PHP の クラス定義
レベル 4197 打鍵
型付きプロパティとコンストラクタを持つ Person クラスを定義し、新しいインスタンスの名前を表示します。
PHP のコード
1class Person { // クラスの外からアクセスできる型付き public プロパティ2 public string $name;3 public int $age;4 // new のときに渡した引数で自動的に呼ばれる5 public function __construct(string $name, int $age) { // $this->name はプロパティ、$name は引数6 $this->name = $name;7 $this->age = $age;8 }9}10 // name が Alice、age が 25 のオブジェクトを作る11$p = new Person("Alice", 25);// -> でオブジェクトのプロパティを読み、「Alice」を表示する12echo $p->name;実行結果
Alice
このコードでタイピング練習ができます。記録はランキングに反映されません。
このコードで使う記号
この PHP のコードで打つ記号と、出てくる回数です。
$× 10;× 6-× 3=× 3>× 3_× 2,× 2"× 2(× 2)× 2