PHP の フルーエント文字列
レベル 5284 打鍵
$thisを返すフルーエントメソッドで文字列操作をチェーンします。
PHP のコード
// 文字列を変更して$thisを返すメソッドを持つラッパー1class S { // プロモーションされたpublicな$vが文字列を保持する2 public function __construct(3 public string $v4 ) {}5 // 大文字に変換する6 public function upper(): static {7 $this->v = strtoupper($this->v); // $thisを返すのでメソッドをつなげられる8 return $this;9 }10 // 前後の空白を取り除く11 public function trim(): static {12 $this->v = trim($this->v);13 return $this;14 }15}16 // 前後に空白のある文字列17$s = new S(" Hello World ");// trim、upperの順につなげて$vを読み、HELLO WORLDと表示する18echo $s->trim()->upper()->v;実行結果
HELLO WORLD
このコードでタイピング練習ができます。記録はランキングに反映されません。
このコードで使う記号
この PHP のコードで打つ記号と、出てくる回数です。
$× 9(× 8)× 8-× 7>× 7;× 6{× 4}× 4=× 3_× 2