요약 - 간단한 계산기
Coddy PHP 여정의 객체 지향 프로그래밍 섹션에 포함된 레슨 — 91개 중 9번째.
챌린지
중급지금까지 배운 모든 OOP 개념을 보여주는 완전한 Calculator 클래스를 calculator.php에 작성하세요. driver.php 파일이 여러분의 클래스를 가져와서 테스트할 것입니다.
여러분의 Calculator 클래스는 다음을 포함해야 합니다:
- 속성(Property): 합계를 저장할
public $result속성 - 생성자(
__construct):$result를0으로 초기화하고"Calculator ready"를 출력 - 소멸자(
__destruct):"Calculator shutting down"을 출력 - 메서드(Methods): 각각
$this를 사용하여 결과를 업데이트하고 반환합니다:add($number)— 결과에 숫자를 더함subtract($number)— 결과에서 숫자를 뺌multiply($number)— 결과에 숫자를 곱함divide($number)— 정수 나눗셈(intdiv)을 사용하여 결과를 숫자로 나눔. 숫자가0인 경우,"Error: Division by zero"를 출력하고 변경되지 않은 결과를 반환clear()— 결과를0으로 리셋getResult()— 현재 결과를 반환
모든 산술 메서드는 연산 후의 새로운 결과를 반환해야 합니다.
사용 예시:
require_once 'calculator.php';
$calc = new Calculator(); // prints: Calculator ready
$calc->add(5); // result becomes 5
$calc->multiply(3); // result becomes 15
$calc->subtract(2); // result becomes 13
$calc->divide(0); // prints: Error: Division by zero (result stays 13)
$calc->divide(2); // result becomes 6 (integer division)
$calc->clear(); // result becomes 0
// When script ends: prints Calculator shutting down직접 해보기
<?php
require_once 'calculator.php';
$testCase = trim(fgets(STDIN));
if ($testCase == "constructor_test") {
$calc = new Calculator();
echo "Initial result: " . $calc->getResult() . "\n";
echo "Calculator created successfully\n";
} elseif ($testCase == "addition_test") {
$calc = new Calculator();
$result1 = $calc->add(10);
echo "After adding 10: " . $result1 . "\n";
$result2 = $calc->add(5);
echo "After adding 5: " . $result2 . "\n";
echo "Final result: " . $calc->getResult() . "\n";
} elseif ($testCase == "subtraction_test") {
$calc = new Calculator();
$calc->add(20);
$result1 = $calc->subtract(8);
echo "After subtracting 8 from 20: " . $result1 . "\n";
$result2 = $calc->subtract(2);
echo "After subtracting 2: " . $result2 . "\n";
echo "Final result: " . $calc->getResult() . "\n";
} elseif ($testCase == "multiplication_test") {
$calc = new Calculator();
$calc->add(5);
$result1 = $calc->multiply(4);
echo "After multiplying by 4: " . $result1 . "\n";
$result2 = $calc->multiply(2);
echo "After multiplying by 2: " . $result2 . "\n";
echo "Final result: " . $calc->getResult() . "\n";
} elseif ($testCase == "division_test") {
$calc = new Calculator();
$calc->add(100);
$result1 = $calc->divide(4);
echo "After dividing by 4: " . $result1 . "\n";
$result2 = $calc->divide(5);
echo "After dividing by 5: " . $result2 . "\n";
echo "Final result: " . $calc->getResult() . "\n";
} elseif ($testCase == "division_by_zero_test") {
$calc = new Calculator();
$calc->add(50);
echo "Initial value: " . $calc->getResult() . "\n";
$result = $calc->divide(0);
echo "Result after division by zero: " . $result . "\n";
echo "Value unchanged: " . $calc->getResult() . "\n";
} elseif ($testCase == "clear_test") {
$calc = new Calculator();
$calc->add(25);
$calc->multiply(3);
echo "Before clear: " . $calc->getResult() . "\n";
$result = $calc->clear();
echo "After clear: " . $result . "\n";
echo "Current result: " . $calc->getResult() . "\n";
} elseif ($testCase == "comprehensive_test") {
$calc = new Calculator();
$calc->add(10);
$calc->multiply(3);
$calc->subtract(5);
$calc->divide(5);
echo "After sequence of operations: " . $calc->getResult() . "\n";
$calc->divide(0);
echo "After division by zero attempt: " . $calc->getResult() . "\n";
$calc->clear();
$calc->add(100);
echo "After clear and add 100: " . $calc->getResult() . "\n";
} else {
echo "Unknown test case\n";
}
객체 지향 프로그래밍의 모든 레슨
8매직 메서드
매직 메서드 소개__toString 및 __debugInfo__get, __set, __isset, __unset__call 및 __callStatic__clone 및 객체 복제__serialize 및 __unserialize요약 - 커스텀 컬렉션