lupus Posted March 31, 2004 Report Share Posted March 31, 2004 Bet tomēr ir dikti aktuāli - kā uztaisīt array lai tajā varētu sasviest un izdabuut objektus. Pašlaik ir nestrādājošs variants: class Letter { $var name; ... } class Alphabet { var $letters; function Alphabet() { readAllLetters(); } function readAllLetters() { ... db stuff ... while (ir vēl db ieraksti) { $this->letters[] = new Letter(); ... te veel vaig izsaukt vienu Letter funkciju - ar [sizeof(arr) -1] to vareetu izdariit, ja atpaziitu ka tas veel ir objekts. } function getAllLetters() { return $this->letters; } } un otrs koda gabaliņš $alp = new Alphabet(); $arr = $alp->getAllLetters(); foreach($arr as $letter){ echo $letter->name; } Lieki piebilst ka pie katra izsaukuma jau iekš array man paziņo ka aiztieku member funkciju ne objektam. Ir kādas idejas? Dikti vaig saprast kur cirvis ziemo... Link to comment Share on other sites More sharing options...
bubu Posted March 31, 2004 Report Share Posted March 31, 2004 buutu labi, ja zem Alphabet konstruktora tu inicializeeto savu $letters shaadi: $letters = array(); a shis gabals tev straadaaja: [sizeof(arr) -1] ? Link to comment Share on other sites More sharing options...
Venom Posted March 31, 2004 Report Share Posted March 31, 2004 neesmu nodarbojies, bet varētu būt ka: objektu jāuztaisa pirms to saglabāt masīvā, resp. nelietot $array[]=new Kautkas; bet $tmp=new Kautkas; $array[]=$tmp; visādam gadījumam: class Alphabet { var $letters=array(); ... foreach($arr as (object) $letter) vai izmantot new stdClass skatīties ar preferably var_dump() vai print_r() kas tef tiek atgrie_z?s_ts var paskatīties dažādas get_object_vars() utml. Link to comment Share on other sites More sharing options...
des Posted March 31, 2004 Report Share Posted March 31, 2004 hvz, nu shitaats kods straadaa: <? class Letter { var $name; function Letter($n) { $this->name=$n; } } class Alpha { var $letters=array(); function WriteStr($s) { for ($x=0;$x<strlen($s);$x++){ $this->letters[]=new Letter($s[$x]); } } function GetLetters() { return $this->letters; } } $a=new Alpha(); $a->WriteStr('blahamuha'); $s=$a->GetLetters(); //print_r($s); foreach ($s as $o) { echo $o->name; } ?> taa kaa cirvis ir kaut kur pa celjam... iepostee visu abu klashu kodu! Link to comment Share on other sites More sharing options...
des Posted March 31, 2004 Report Share Posted March 31, 2004 Veel jo vairaak. Arii shaads kods straadaa: $a=new Alpha(); $a->WriteStr('blahamuha'); $b=new Alpha(); $b->WriteStr('killemall'); $r=array($a,$b); foreach ($r as $rs) { $gz=$rs->GetLetters(); foreach ($gz as $o) { echo $o->name; } } Taa kaa toch taisi print_r($alp) un debago, kas Tev tur notiek :) Link to comment Share on other sites More sharing options...
Recommended Posts