
SplFixedArray::__construct 메소드에서 크기를 체크하여 오류가 발생할 수 있는 경우를 예상하고, 오류 메시지를 출력하는 방법은 다음과 같습니다.
1. 크기가 0보다 작거나 음수일 때 오류 메시지를 출력합니다.
2. 크기가 0보다 작거나 음수일 때 Exception 예외를 발생시킵니다.
3. 크기가 0보다 작거나 음수일 때 오류 메시지를 출력하고, Exception 예외를 발생시킵니다.
예를 들어, 다음 코드는 SplFixedArray::__construct 메소드에서 크기를 체크하여 크기가 0보다 작거나 음수일 때 오류 메시지를 출력하고 Exception 예외를 발생시킵니다.
#hostingforum.kr
php
class MySplFixedArray extends SplFixedArray {
public function __construct($size) {
if ($size < 0) {
throw new Exception('크기 must be a non-negative integer');
}
parent::__construct($size);
}
}
try {
$mySplFixedArray = new MySplFixedArray(-10);
} catch (Exception $e) {
echo $e->getMessage(); // 크기 must be a non-negative integer
}
또한, 다음 코드는 SplFixedArray::__construct 메소드에서 크기를 체크하여 크기가 0보다 작거나 음수일 때 오류 메시지를 출력하고 Exception 예외를 발생시키는 방법을 보여줍니다.
#hostingforum.kr
php
class MySplFixedArray extends SplFixedArray {
public function __construct($size) {
if ($size < 0) {
echo '크기 must be a non-negative integer';
throw new Exception('크기 must be a non-negative integer');
}
parent::__construct($size);
}
}
try {
$mySplFixedArray = new MySplFixedArray(-10);
} catch (Exception $e) {
echo $e->getMessage(); // 크기 must be a non-negative integer
}
2025-03-12 22:10