
Collection 클래스의 `__construct` 메서드는 클래스의 초기화 역할을 합니다. 이 메서드는 클래스가 생성될 때 자동으로 호출되며, 클래스의 속성을 초기화합니다.
`__construct` 메서드에서 사용되는 매개변수는 다음과 같습니다.
- `$items`: 이 매개변수는 Collection 클래스에 저장할 아이템의 목록입니다. 예를 들어, `$items = [1, 2, 3, 4, 5];`와 같이 사용할 수 있습니다.
이 매개변수는 Collection 클래스의 속성을 초기화하는 데 사용됩니다. 예를 들어, `$items` 매개변수를 사용하여 Collection 클래스의 `items` 속성을 초기화할 수 있습니다.
#hostingforum.kr
php
class Collection {
private $items;
public function __construct($items) {
$this->items = $items;
}
public function getItems() {
return $this->items;
}
}
$collection = new Collection([1, 2, 3, 4, 5]);
echo json_encode($collection->getItems()); // [1, 2, 3, 4, 5]
이 예제에서 `$items` 매개변수는 Collection 클래스의 `items` 속성을 초기화합니다. 이 속성은 Collection 클래스의 아이템을 저장하는 데 사용됩니다.
2025-04-28 01:32