Einzelnen Beitrag anzeigen

Benutzerbild von Luckie
Luckie

Registriert seit: 29. Mai 2002
37.621 Beiträge
 
Delphi 2006 Professional
 
#6

AW: [PHP] Objektliste

  Alt 3. Feb 2012, 19:23
Äh, ja. Verursacht Blitzdingsen eigentlich Krebs? *blitz*

Aber irgendwas verstehe ich bei den Array Funktionen noch nicht so richtig. Ich dachte mit array_push wird ein neues Element an das Array angehangen. Allerdings, wenn ich mir die Liste ausgeben lasse, beinhalten alle Elemente nur das zu letzt hinzugefügte Objekt.

Aktueller Code:
Code:
<?php
   class Contact
   {
      private $Name = "";
      private $Vorname = "";
      
      public function SetName($Name) {
         $this->Name = $Name;
      }
      
      public function GetName() {
         return $this->Name;
      }
      
      public function SetVorname($Vorname) {
         $this->Vorname = $Vorname;
      }
      
      public function GetVorname() {
         return $this->Vorname;
      }
   }
?>

<?php
   class ContactList {
      
      private $Contact;
      private $List = array();
      
      public function SetContact($Contact) {
         $this->Contact = $Contact;
      }
      
      public function Add($Contact) {
         array_push($this->List, $Contact);
         echo $Contact->GetName()."<br>";
      }
      
      public function Contacts() {
         return $this->List;         
      }
   }
?>


<?php
   include("Contact.php");
   include("ContactList.php");

    $Contact = new Contact();
    $ContactList = new ContactList();
    
    $Contact->SetName("Puff");
    $Contact->SetVorname("Michael");
    $ContactList->Add($Contact);
    $Contact->SetName("Müller");
    $Contact->SetVorname("Karl");
    $ContactList->Add($Contact);
    $Contact->SetName("Schmidt");
    $Contact->SetVorname("Hans");
    $ContactList->Add($Contact);
    
    echo "<pre>", print_r($ContactList), "</pre>";
    
    foreach($ContactList->Contacts() as $Value) {
       echo $Value->GetName().", ".$Value->GetVorname()."<br>";
       //print_r($Value);
    }
?>
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat