ERLEDIGT: werte an __constructor übergeben

Schon festgestellt? Dem Forum sind gerade alle Interaktionsmöglichkeiten genommen. :(

Mehr dazu unter: Abstimmung über das Forum

  • ich habe in der klasse 'FORM' den konstruktor an dem ich werte übergeben möchte

    PHP
    1. <span class="syntaxdefault"><br /></span><span class="syntaxcomment">#klasse<br /></span><span class="syntaxdefault">class FORM </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment">#konstruktor<br /></span><span class="syntaxdefault">    public function __construct</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$action</span><span class="syntaxkeyword">=</span><span class="syntaxstring">""</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $method</span><span class="syntaxkeyword">=</span><span class="syntaxstring">""</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $name</span><span class="syntaxkeyword">=</span><span class="syntaxstring">""</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">        </span><span class="syntaxkeyword">(!</span><span class="syntaxdefault">$action</span><span class="syntaxkeyword">)?</span><span class="syntaxdefault">$action</span><span class="syntaxkeyword">:</span><span class="syntaxdefault">$action</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">$self</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">        echo </span><span class="syntaxstring">'<form action="'</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$action</span><span class="syntaxkeyword">.</span><span class="syntaxstring">'" method="'</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$method</span><span class="syntaxkeyword">.</span><span class="syntaxstring">'" name="'</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$name</span><span class="syntaxkeyword">.</span><span class="syntaxstring">'">'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">    </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment">#dekonstruktor<br /></span><span class="syntaxdefault">    public function __deconstruct</span><span class="syntaxkeyword">()</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">        echo </span><span class="syntaxstring">'</form>'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">    </span><span class="syntaxkeyword">}<br />}<br /></span><span class="syntaxcomment">#objekt instanzieren<br /></span><span class="syntaxdefault">$form</span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> new FORM</span><span class="syntaxkeyword">;<br /></span><span class="syntaxcomment">#werte übergeben - :/ funktioniert nicht<br /></span><span class="syntaxdefault">$form</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">__construct</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'bbbbbbbb'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'get'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'HHHHHH'</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">$form</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">__deconstruct</span><span class="syntaxkeyword">();<br /></span><span class="syntaxdefault"> </span>


    das form html tag wird im quellcode angezeigt nur die werte werden nicht übermittelt


    habt ihr eine idee


    gruß
    peter

  • __construct und __deconstruct führt man nicht aus ;)
    __construct wird automatisch beim erzeugen "angewendet" :D
    und __deconstruct eben mein "zerstören" eines Objekts^^


    Richtig also:

    PHP
    1. <span class="syntaxdefault">$form</span><span class="syntaxkeyword">= new </span><span class="syntaxdefault">FORM</span><span class="syntaxkeyword">(</span><span class="syntaxstring">"aaaaa"</span><span class="syntaxkeyword">, </span><span class="syntaxstring">"bbbb"</span><span class="syntaxkeyword">, </span><span class="syntaxstring">"cccc"</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">destroy</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$form</span><span class="syntaxkeyword">); </span><span class="syntaxdefault"></span>
  • Wenn das Objektorientierte Programmierung sein soll dann fehlt da so einiges.


    Es heißt z.B. nicht __deconstruct sondern __destruct.


    In einer Klasse erfolgt keine Ausgabe, nur return Werte die späte nach Aufruf der Klasse ausgegeben werden.


    Den Konstruktor und Destruktor rufst du nicht als FUnktion auf, __construct gibst du beim erstellen der Klasse mit ($klasse = new KLASSE(werte);), __destruct wird ausgeführt wenn du die Variable zerstörst (unset($klasse)).


    Du hast die Variable $self nicht definiert?


    So wie du das meinst:


    PHP
    1. <span class="syntaxhtml"><br /><span class="syntaxdefault"><?php<br /><br />  class form<br />  </span><span class="syntaxkeyword">{<br /><br /></span><span class="syntaxdefault">    public $action</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">    public $method</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">    public $name</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">    public $returnString </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">''</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">    public function __construct</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$ac </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">''</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $me </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">''</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $na </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> </span><span class="syntaxstring">''</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">    </span><span class="syntaxkeyword">{<br /><br /></span><span class="syntaxdefault">      if</span><span class="syntaxkeyword">(!</span><span class="syntaxdefault">$ac</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">        $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">action </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $_SERVER</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'PHP_SELF'</span><span class="syntaxkeyword">];<br /></span><span class="syntaxdefault">      else<br />        $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">action </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $ac</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">      $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">method </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $me</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">      $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">name </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> $na</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">      return true</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">    </span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">    public function generateString</span><span class="syntaxkeyword">()<br /></span><span class="syntaxdefault">    </span><span class="syntaxkeyword">{<br /><br /></span><span class="syntaxdefault">      $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">returnString </span><span class="syntaxkeyword">.=</span><span class="syntaxdefault"> </span><span class="syntaxstring">'<form action="'</span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">action </span><span class="syntaxkeyword">.</span><span class="syntaxstring">'"'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">      if</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">method</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">        $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">returnString </span><span class="syntaxkeyword">.=</span><span class="syntaxdefault"> </span><span class="syntaxstring">' method="'</span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">method </span><span class="syntaxkeyword">.</span><span class="syntaxstring">'"'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">      if</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">name</span><span class="syntaxkeyword">)<br /></span><span class="syntaxdefault">        $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">returnString </span><span class="syntaxkeyword">.=</span><span class="syntaxdefault"> </span><span class="syntaxstring">' name="'</span><span class="syntaxkeyword">.</span><span class="syntaxdefault"> $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">name </span><span class="syntaxkeyword">.</span><span class="syntaxstring">'">'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">      else<br />        $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">returnString </span><span class="syntaxkeyword">.=</span><span class="syntaxdefault"> </span><span class="syntaxstring">'>'</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">      return $this</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">returnString</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">    </span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">    public function __destruct</span><span class="syntaxkeyword">()<br /></span><span class="syntaxdefault">    </span><span class="syntaxkeyword">{<br /><br /></span><span class="syntaxdefault">      print </span><span class="syntaxstring">'</form>'</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">      return true</span><span class="syntaxkeyword">;<br /><br /></span><span class="syntaxdefault">    </span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">  </span><span class="syntaxkeyword">}<br /><br /></span><span class="syntaxdefault">  $a </span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> new form</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'a'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'b'</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> </span><span class="syntaxstring">'c'</span><span class="syntaxkeyword">);<br /></span><span class="syntaxdefault">  print $a</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">generateString</span><span class="syntaxkeyword">();<br /></span><span class="syntaxdefault">  unset</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$a</span><span class="syntaxkeyword">);<br /><br /></span><span class="syntaxdefault">?><br /></span></span>
  • süß ... nicht :)
    immer diese autoditakten spätzünder :)



    erstmal danke für euer konstruktives feedback
    beim einschlafen dachte ich noch darüber nach und in der früh nahm ich mein buch in die hand und siehe da: auf der fünften! seite steht die lösung


    ich hab es nochmal geschrieben, aber wieder mit echo in der methode
    es funktioniert so wie ich es mir vorstelle


    auch die variablen habe ich nicht definiert
    in den klassen im www hab ich es oft so gesehen:
    in den methoden verwendete variablen müssen nicht wie attribute einer klasse behandelt werden
    ich dachte zuerst, dass das nicht korrekt ist, aber... die klassen funktionieren ;)


    PHP
    1. <span class="syntaxdefault"><br /></span><span class="syntaxcomment">#Klasse<br /></span><span class="syntaxdefault">class FORM </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment">#construktor methode wird beim instanzieren der Klasse ausgeführt<br /></span><span class="syntaxdefault">    public function __construct</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$action</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">FALSE</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $method</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">false</span><span class="syntaxkeyword">,</span><span class="syntaxdefault"> $name</span><span class="syntaxkeyword">=</span><span class="syntaxdefault">false</span><span class="syntaxkeyword">)</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">        </span><span class="syntaxcomment">#action = <br /></span><span class="syntaxdefault">        echo</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$action</span><span class="syntaxkeyword">)?</span><span class="syntaxdefault">$action</span><span class="syntaxkeyword">:</span><span class="syntaxdefault">$_SERVER</span><span class="syntaxkeyword">[</span><span class="syntaxstring">'PHP_SELF'</span><span class="syntaxkeyword">];<br /></span><span class="syntaxdefault">        </span><span class="syntaxcomment">#method =<br /></span><span class="syntaxdefault">        echo</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$method</span><span class="syntaxkeyword">)?</span><span class="syntaxdefault">$method</span><span class="syntaxkeyword">:</span><span class="syntaxstring">'get'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">        </span><span class="syntaxcomment">#formName =<br /></span><span class="syntaxdefault">        echo</span><span class="syntaxkeyword">(</span><span class="syntaxdefault">$name</span><span class="syntaxkeyword">)?</span><span class="syntaxdefault">$name</span><span class="syntaxkeyword">:</span><span class="syntaxstring">'form'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">        </span><span class="syntaxcomment">#ausgabe<br /></span><span class="syntaxdefault">        echo </span><span class="syntaxstring">'<form action="'</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$action</span><span class="syntaxkeyword">.</span><span class="syntaxstring">'" method="'</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$method</span><span class="syntaxkeyword">.</span><span class="syntaxstring">'" name="'</span><span class="syntaxkeyword">.</span><span class="syntaxdefault">$name</span><span class="syntaxkeyword">.</span><span class="syntaxstring">'">'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">    </span><span class="syntaxkeyword">}<br /></span><span class="syntaxdefault">    </span><span class="syntaxcomment">#wird beim löschen der klasse ausgeführt<br /></span><span class="syntaxdefault">    public function __destruct</span><span class="syntaxkeyword">()</span><span class="syntaxdefault"> </span><span class="syntaxkeyword">{<br /></span><span class="syntaxdefault">        echo </span><span class="syntaxstring">'</form>'</span><span class="syntaxkeyword">;<br /></span><span class="syntaxdefault">    </span><span class="syntaxkeyword">}<br />}<br /></span><span class="syntaxcomment">#instanzierung<br /></span><span class="syntaxdefault">$form</span><span class="syntaxkeyword">=</span><span class="syntaxdefault"> new FORM</span><span class="syntaxkeyword">(</span><span class="syntaxstring">'base.php'</span><span class="syntaxkeyword">);<br /></span><span class="syntaxcomment">#löschen<br /></span><span class="syntaxkeyword">unset(</span><span class="syntaxdefault">$form</span><span class="syntaxkeyword">);<br /><br /></span><span class="syntaxdefault"> </span>


    vielen dank für die hilfe =)


    gruß
    peter

  • Es ist schön und gut, aber es geht bei OOP nicht darum ob die Klasse funktioniert.


    PHP
    1. <span class="syntaxdefault"><br /><br /> </span><span class="syntaxkeyword">class </span><span class="syntaxdefault">nix <br /> </span><span class="syntaxkeyword">{<br /><br /> </span><span class="syntaxdefault">public </span><span class="syntaxkeyword">function </span><span class="syntaxdefault">nothing</span><span class="syntaxkeyword">()<br /> {<br /> print </span><span class="syntaxdefault">1</span><span class="syntaxkeyword">;<br /> }<br /><br /> }<br /><br /> </span><span class="syntaxdefault">$var </span><span class="syntaxkeyword">= new </span><span class="syntaxdefault">nix</span><span class="syntaxkeyword">;<br /> </span><span class="syntaxdefault">$var</span><span class="syntaxkeyword">-></span><span class="syntaxdefault">nothing</span><span class="syntaxkeyword">();<br /><br /> </span><span class="syntaxdefault"></span>


    Funktioniert auch, ist aber total sinnlos und kein bisschen OOP.


    Semantik ist da genau so wichtig wie bei HTML, nur das es niemand sieht!