--- id: php-oop-inheritance title: "PHP OOP Inheritance" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["extends", "final keyword", "PHP 상속"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.9 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["php", "programming", "w3schools", "oop", "inheritance"] raw_sources: ["https://www.w3schools.com/php/php_oop_inheritance.asp"] applied_in: [] github_commit: "" --- # [[PHP OOP Inheritance]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) Calling a `protected` method from OUTSIDE the class fails, but calling that SAME protected method from WITHIN a subclass's own method (via `$this->intro()`) succeeds β€” the exact rule from access modifiers applies identically to methods, and the source proves it with a direct pass/fail comparison of the two call sites. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`extends`** β€” inherits public and protected properties/methods from a parent class. [S1] - **Private members are NOT inherited** β€” a child class cannot access a parent's private methods/properties at all. [S1] - **Method overriding** β€” redefining a method with the same name in the child class replaces the parent's version. [S1] - **`final` on a class** β€” prevents any class from extending it. [S1] - **`final` on a method** β€” prevents child classes from overriding that specific method. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Basic inheritance: `class Fruit { public $name; public $color; public function __construct($name, $color) {...} public function intro() {...} } class Strawberry extends Fruit { public function message() {...} } $strawberry = new Strawberry("Strawberry", "red"); $strawberry->intro(); $strawberry->message();` β€” Strawberry uses Fruit's constructor/intro plus its own message(). [S1] - Protected method fails from outside: `$strawberry->intro(); // ERROR if intro() is protected`. [S1] - Protected method works from inside a child method: `public function message() { $this->intro(); }` β€” calling `$this->intro()` from within `message()` succeeds. [S1] - Overriding: child class `Strawberry` redefines `__construct()` and `intro()` with the same names, completely replacing the parent's versions (adding a `$weight` property along the way). [S1] - `final` prevents inheritance: `final class Fruit {...} class Strawberry extends Fruit {...} // error`. [S1] - `final` prevents method override: `class Fruit { final public function intro() {...} } class Strawberry extends Fruit { public function intro() {...} // error }`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **protected λ©”μ„œλ“œ 호좜 μœ„μΉ˜μ— λ”°λ₯Έ μ„±νŒ¨**: 클래슀 μ™ΈλΆ€μ—μ„œ ν˜ΈμΆœν•˜λ©΄ μ—λŸ¬, νŒŒμƒ 클래슀 λ‚΄λΆ€ λ©”μ„œλ“œμ—μ„œ $this둜 ν˜ΈμΆœν•˜λ©΄ μ„±κ³΅ν•œλ‹€λŠ” 점이 두 λŒ€λΉ„ 예제둜 직접 증λͺ…됨. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” Strawberryκ°€ Fruit의 μƒμ„±μž/λ©”μ„œλ“œλ₯Ό μž¬μ •μ˜(override)ν•˜λ©° weight 속성을 μΆ”κ°€ν•˜λŠ” 것이 μ‹€μ „ μ˜€λ²„λΌμ΄λ”©μ˜ λŒ€ν‘œ 사둀닀. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Overriding a parent's constructor and method (PHP): ```php class Fruit { public $name; public $color; public function __construct($name, $color) { $this->name = $name; $this->color = $color; } public function intro() { echo "The fruit is $this->name and the color is $this->color."; } } class Strawberry extends Fruit { public $weight; public function __construct($name, $color, $weight) { $this->name = $name; $this->color = $color; $this->weight = $weight; } public function intro() { echo "A $this->name is $this->color, and the weight is $this->weight gram."; } } ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[PHP Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[PHP OOP Access Modifiers]], [[PHP OOP Constants]], [[PHP OOP Classes Abstract]] - **μ°Έμ‘° λ§₯락:** 클래슀 상속과 λ©”μ„œλ“œ μ˜€λ²„λΌμ΄λ”© β€” 클래슀 μƒμˆ˜(Constants) μ±•ν„°λ‘œ 이어짐. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” PHP OOP - Inheritance β€” https://www.w3schools.com/php/php_oop_inheritance.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "PHP OOP Inheritance" page (Astra wiki-curation, P-Reinforce v3.1 format).