--- id: php-oop-constructor title: "PHP OOP Constructor" category: "Programming_Language" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["__construct()", "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", "constructor"] raw_sources: ["https://www.w3schools.com/php/php_oop_constructor.asp"] applied_in: [] github_commit: "" --- # [[PHP OOP Constructor]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) `__construct()` eliminates the need to call a separate setter method after object creation β€” instead of `new Fruit(); $apple->set_details(...)` (two steps), the constructor lets `new Fruit('Apple', 'Red')` initialize properties in ONE step, directly reducing the code from the previous chapter's two-call pattern. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **`__construct($params)`** β€” a special method automatically invoked whenever `new` creates an object; name starts with double underscore. [S1] - **Accepts arguments** β€” passed at creation time: `new Fruit("Apple", "Red")`. [S1] - **Purpose** β€” dynamic initialization, replacing a separate setter call. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Constructor-based initialization: `class Fruit { public $name; public $color; function __construct($name, $color) { $this->name = $name; $this->color = $color; } function get_details() { echo "Name: " . $this->name . ". Color: " . $this->color .".
"; } } $apple = new Fruit('Apple', 'Red'); $apple->get_details();`. [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) μ†ŒμŠ€μ—μ„œ λͺ¨μˆœλ˜λŠ” μ •λ³΄λŠ” λ°œκ²¬λ˜μ§€ μ•ŠμŒ. ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” μƒμ„±μžλ‘œ 이름/색상을 ν•œ λ²ˆμ— μ΄ˆκΈ°ν™”ν•˜λŠ” 것이 setter λ©”μ„œλ“œ ν˜ΈμΆœμ„ μ€„μ΄λŠ” ν‘œμ€€ νŒ¨ν„΄μ΄λ‹€. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) Constructor-based initialization in one step (PHP): ```php class Fruit { public $name; public $color; function __construct($name, $color) { $this->name = $name; $this->color = $color; } } $apple = new Fruit('Apple', 'Red'); // initialized directly, no separate setter call ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[PHP Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[PHP OOP Classes Objects]], [[PHP OOP Destructor]] - **μ°Έμ‘° λ§₯락:** 객체 μ΄ˆκΈ°ν™” λ©”μ„œλ“œ β€” μ†Œλ©Έμž(Destructor) μ±•ν„°λ‘œ 직접 이어짐(μƒμ„±μžμ˜ λ°˜λŒ€ κ°œλ…). ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” PHP OOP - Constructor Method β€” https://www.w3schools.com/php/php_oop_constructor.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "PHP OOP Constructor Method" page (Astra wiki-curation, P-Reinforce v3.1 format).