ugrás a tartalomhoz

OOP Constructor oroklodese

zoliky · 2008. Aug. 27. (Sze), 15.42
Kezdo vagyok OOP teren. Elolvastam par dokumentaciot, de valamit nem igazan ertek! Itt van a kovetkezo kod:
  1. class ShopProduct {  
  2.    public $title;  
  3.    public $producerMainName;  
  4.    public $producerFirstName;  
  5.    public $price;  
  6.   
  7.    function __construct( $title$firstName$mainName$price ) {  
  8.        $this->title = $title;  
  9.        $this->producerFirstName = $firstName;  
  10.        $this->producerMainName = $mainName;  
  11.        $this->price = $price;  
  12.    }  
  13.   
  14.    function getProducer() {  
  15.        return "{$this->producerFirstName}"" {$this->producerMainName}";  
  16.    }  
  17.   
  18.    function getSummaryLine() {  
  19.        $base = "{$this->title} ( {$this->producerMainName}, ";  
  20.        $base .= "{$this->producerFirstName} )";   
  21.        return $base;  
  22.    }  
  23. }  
es itt van a gyerek (child) class amely orokol mindent a ShopProduct-tol. A constructort is orokli!
Na most en szeretnek egy sajat consztruktort letrehozni a gyerek class-ban.
  1. class CdProduct extends ShopProduct {  
  2.     public $playLength;  
  3.       
  4.     function __construct($title$firstName$mainName$price$playLength) {  
  5.         parent::__construct($title$firstName$mainName$price);  
  6.         $this->playLength = $playLength;  
  7.     }  
  8.       
  9.     function getPlayLength() {  
  10.         return $this->playLength;  
  11.     }  
  12.   
  13. }  
Azt nem ertem, hogy miert kell ertesiteni a szulot a kovetkezo kodal ?
  1. parent::__construct($title$firstName$mainName$price);  
Ha nem ertesitem a gyerek elveszti azokat a constructor adatokat amiket orokol az apatol?
Koszonom!
 
1

RTFM

Max Logan · 2008. Aug. 27. (Sze), 15.50
PHP: Constructors and Destructors

Note: Parent constructors are not called implicitly if the child class defines a constructor. In order to run a parent constructor, a call to parent::__construct() within the child constructor is required.

Tehát automatikusan nem hívódik meg a szülő konstruktora.
2

ok

zoliky · 2008. Aug. 27. (Sze), 16.05
Koszi!
3

OOP elv megsertese

zmb · 2008. Aug. 27. (Sze), 19.59
Off: Soha, de soha deklaraljal tagvaltozot publikus lathatosaggal. Senkinek semmi koze, hogy egy objektum hogyan, es milyen adatokat tarol magaban.
4

mert te

Fraki · 2008. Aug. 27. (Sze), 23.21
„Azt nem ertem, hogy miert kell ertesiteni a szulot a kovetkezo kodal ?”

Mert te tudod, hogy a származtatott konsturktorban mikor akarod meghívni a szülő konstruktorát (elején/végén), ez esetfüggő.
5

illik a szülő konstruktorát meghívni először

Hodicska Gergely · 2008. Aug. 28. (Cs), 00.53
Általában illik kapásból meghívni a szülő konstruktorát (fordítotja igaz a destruktorra). A konstruktor feladata, hogy inicializálja az adott objektumot. Az öröklés az ősosztály működését bővíti ki, ha nem egyből a szülő konstruktorát hívod meg, akkor a kód egy nem inicializált (nem konzisztens állapotban lévő) objektummal fog dolgozni.

Ezért bizonyos nyelvek esetén (ha jól rémlik, akkor talán C++, de ebben bőven nem vagyok bizots) a fordító sír is, ha nem az első sor az ős konstruktotának meghívása, vagy automatikusan meghívja, ha nem az lenne (pl. java, ha van paraméter nélküli konstruktora az ősnek).


Üdv,
Felhő