AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Thema durchsuchen
Ansicht
Themen-Optionen

[PHP] Objektliste

Ein Thema von Luckie · begonnen am 3. Feb 2012 · letzter Beitrag vom 3. Feb 2012
Antwort Antwort
Benutzerbild von Luckie
Luckie

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

[PHP] Objektliste

  Alt 3. Feb 2012, 18:45
Ich überarbeite gerade ein privates Projekt und versuche gerade eine Objektliste zu erstellen.

Klasse Conatct:
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 $tis->Vorname;
      }
   }
?>
Klasse ContactList:
Code:
<?php
   class ContactList {
      
      private $Contact;
      private $List = array();
      
      public function SetContact($Contact) {
         $this->Contact = $Contact;
      }
      
      public function Add($Contact) {
         array_push($this->List, $Contact);
      }
   }
?>
Und der test:
Code:
<?php
   include("Contact.php");
   include("ContactList.php");

    $Contact = new Contact();
    $ContactList = new ContactList();
    
    $Contact->SetName("Puff");
    $Contact->SetVorname("Michael");
    $ContactList->Add($ContactList);
    $Contact->SetName("Müller");
    $Contact->SetVorname("Karl");
    $ContactList->Add($ContactList);
    $Contact->SetName("Schmidt");
    $Contact->SetVorname("Hans");
    $ContactList->Add($ContactList);
    
    print_r($ContactList);
//    foreach($ContactList as $Contact) {
//       echo $Contact->GetName().", ".$Contact->GetVorname()."<br>";
//    }
?>
print_r gibt mir aus:
Code:
ContactList Object ( [Contact:ContactList:private] =>[List:ContactList:private] => Array ( [0] => ContactList Object *RECURSION* [1] => ContactList Object *RECURSION* [2] => ContactList Object *RECURSION* ) )
Und die auskommentierte Schleife gibt mir gar nichts aus.

Ja, was mache ich da falsch? Ich habe leider keinen Plan.
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
Daniel
(Co-Admin)

Registriert seit: 30. Mai 2002
Ort: Hamburg
13.919 Beiträge
 
Delphi 10.4 Sydney
 
#2

AW: [PHP] Objektliste

  Alt 3. Feb 2012, 18:51

$ContactList->Add($ContactList); Du fügst die Liste der Liste hinzu? Müsstest Du da als Parameter nicht einfach nur $Contact übergeben?
Daniel R. Wolf
mit Grüßen aus Hamburg
  Mit Zitat antworten Zitat
Benutzerbild von Luckie
Luckie

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

AW: [PHP] Objektliste

  Alt 3. Feb 2012, 18:57
OK. Gucken Sie mal auf die Spitze dieses Stiftes: *blitz*

Aber die Schleife gibt mir immer noch nichts aus?

Und gibt es einen anderen Befehl als print_r, der ein Array etwas schöner, übersichtlicher formatiert ausgibt?
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
Daniel
(Co-Admin)

Registriert seit: 30. Mai 2002
Ort: Hamburg
13.919 Beiträge
 
Delphi 10.4 Sydney
 
#4

AW: [PHP] Objektliste

  Alt 3. Feb 2012, 19:00
Und gibt es einen anderen Befehl als print_r, der ein Array etwas schöner, übersichtlicher formatiert ausgibt?
Ich mache da immer echo '<pre>', print_r($dingsbums), '</pre>'; . "Schön" im eigentlichen Sinne ist vielleicht noch etwas anderes, aber die Darstellung wird schon deutlich besser.
Daniel R. Wolf
mit Grüßen aus Hamburg
  Mit Zitat antworten Zitat
Daniel
(Co-Admin)

Registriert seit: 30. Mai 2002
Ort: Hamburg
13.919 Beiträge
 
Delphi 10.4 Sydney
 
#5

AW: [PHP] Objektliste

  Alt 3. Feb 2012, 19:03
Aber die Schleife gibt mir immer noch nichts aus?
Naja, ContactList ist "nur" irgendeine Klasse, die von ihren Daten erstmal nichts nach aussen hin preisgibt. Und ContactList selbst ist auch kein Array, über das man laufen könnte - diese Klasse enthält lediglich ein Array.
Daniel R. Wolf
mit Grüßen aus Hamburg
  Mit Zitat antworten Zitat
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
Benutzerbild von Luckie
Luckie

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

AW: [PHP] Objektliste

  Alt 3. Feb 2012, 19:27
Ich sollte den Whiskey beim Programmieren weglassen:
Code:
    $Contact = new Contact();
    $Contact->SetName("Puff");
    $Contact->SetVorname("Michael");
    $ContactList->Add($Contact);
    $Contact = new Contact();
    $Contact->SetName("Müller");
    $Contact->SetVorname("Karl");
    $ContactList->Add($Contact);
    $Contact = new Contact();
    $Contact->SetName("Schmidt");
    $Contact->SetVorname("Hans");
    $ContactList->Add($Contact);
Michael
Ein Teil meines Codes würde euch verunsichern.
  Mit Zitat antworten Zitat
Antwort Antwort


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 15:08 Uhr.
Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz