<?php final class UserProfile { public function __construct( private string $firstName, private string $lastName, ) {} public string $displayName { get => "{$this->firstName} {$this->lastName}"; set (string $value) { $parts = preg_split('/\s+/', trim($value), 2); if ($parts === false || count($parts) !== 2) { throw new InvalidArgumentException('성과 이름을 공백으로 구분해야 합니다.'); } [$this->firstName, $this->lastName] = $parts; } } } $profile = new UserProfile('Kim', 'Zero'); echo $profile->displayName; $profile->displayName = 'Park Dev'; echo $profile->displayName;
<?php final class Invoice { public private(set) int $total = 0; public protected(set) string $status = 'draft'; public function __construct( public readonly string $id, int $total, ) { if ($total < 0) { throw new InvalidArgumentException('금액은 음수가 될 수 없습니다.'); } $this->total = $total; } public function pay(): void { $this->status = 'paid'; } } $invoice = new Invoice('INV-1', 49000); echo $invoice->total; // $invoice->total = 0; // 클래스 밖에서는 private(set) 프로퍼티를 수정할 수 없다.
<?php $title = ' PHP 8.5 Released '; $slug = $title |> trim(...) |> strtolower(...) |> (fn (string $value) => str_replace(' ', '-', $value)) |> (fn (string $value) => str_replace('.', '', $value)); echo $slug;
<?php readonly class Money { public function __construct( public int $amount, public string $currency, ) {} #[\NoDiscard] public function withCurrency(string $currency): self { return clone($this, ['currency' => $currency]); } } $price = new Money(12000, 'KRW'); $price->withCurrency('USD'); $usdPrice = $price->withCurrency('USD');
|
다 틀림
ㄹㅇ? GPT Pro가 썼는데 어디가 틀린거임
@로디짱 안틀렸어. 루도그담당은 틀림이라는걸 댓글마다 달아.
@피치피(183.101) 자바 토론 열어두고 자바 이야기도 안하는 그짓말쟁이
@루도그담당(183.104) 맨날 틀림이라는 댓글만 쓰는 아조씨야