Sponsor-Board.de
[Help] PHP OOP - Weitergabe einer Variable

+- Sponsor-Board.de (https://www.sponsor-board.de)
+-- Forum: Community (/forumdisplay.php?fid=56)
+--- Forum: Hilfe (/forumdisplay.php?fid=102)
+---- Forum: Scripting (/forumdisplay.php?fid=108)
+---- Thema: [Help] PHP OOP - Weitergabe einer Variable (/showthread.php?tid=40101)


[Help] PHP OOP - Weitergabe einer Variable - Sysix - 25.06.2013 13:39

Hallo leute

ich kapier gereade nicht wieso das nicht funktioniert:

ich habe eine Methode und bei der Frag ich hab, was ich gerade ausgewählt habe (von 3 Variablen) und gebe diese Variable auch weiter.

Nur Leider ändert er die einstellungen nicht so wie ich es will :/



Code:
protected $thead = array();
    protected $tfoot = array();
    protected $tbody_array = array();
    
    protected $current_section;
    
    public function addSection($section, $class = '', $attributes = array() ) {
        
        switch ($section) {
            case 'thead':
                $this->current_section = 'thead';                
                break;
            case 'tfoot':
                $this->current_section = 'tfoot';
                break;
                
            default: // tbody
                $this->tbody_array[] = array();
                $this->current_section = count($this->tbody_array)-1;
        }
        
        
        $ref = &$this->getCurrentSection();
        
        if(!empty($class))
            $attributes['class'] = $class;
    
        $ref['attr'] = $attributes;
        $ref['rows'] = array();
        
    }
    
    protected function getCurrentSection() {
        
        if($this->current_section == 'thead') {
            return $this->thead;    
        } elseif($this->current_section == 'tfoot') {
            return $this->tfoot;    
        } else {
            return $this->tbody_array[$this->current_section];    
        }
    }


leider macht er nach print_r keine Ausgabe :/

Falls jemand frägt wieso in einer Extra Methode: ich brauche die Abfrage später noch mehrmals

Gruß Sysix


RE: [Help] PHP OOP - Weitergabe einer Variable - Dennis - 25.06.2013 13:55

da du doch ein etwas eingesessener programmierer bist:
standardpozedere mit error_reporting etc. mal gemacht?

Mach an der ausgabe doch mal ne ausgabe, einfach per echo, ob der überhaupt in die if-anweisung reinspringt.


RE: [Help] PHP OOP - Weitergabe einer Variable - Sysix - 25.06.2013 14:03

schon gemacht ^^

Zitat:
leider macht er nach print_r keine Ausgabe :/

[/quote]
Fehler tut er mir jetzt keine raushauen

Code:
table Object (
[thead:protected] => Array ( )
[tfoot:protected] => Array ( )
[tbody_array:protected] => Array ( [0] => Array ( ) )
[current_section:protected] => 0
)




RE: [Help] PHP OOP - Weitergabe einer Variable - Dennis - 25.06.2013 14:13

schreib mal spaßeshalber $this->current_section="test";
vor die switchanweisung und schau nochmal, was der ausgibt.

kann es dort übrigens sein, dass ein break beim default fehlt?


RE: [Help] PHP OOP - Weitergabe einer Variable - Sysix - 25.06.2013 14:17

Zitat:
Notice: Undefined index: test in


Code:
protected function getCurrentSection() {
        
         $this->current_section="test";
        
        if($this->current_section == 'thead') {
            return $this->thead;    
        } elseif($this->current_section == 'tfoot') {
            return $this->tfoot;    
        } else {
            return $this->tbody_array[$this->current_section];    
        }
    }


sprich er returnt:

Code:
$this->tbody_array['text']


nein ist eigentlich egal, da dies sowieso die letzte Zeile ist. Break beendet einfach nur die Schleife.. war bei thead/tfoot nur wichtig da er dann nichtmehr das default durchkämmen muss Smile


RE: [Help] PHP OOP - Weitergabe einer Variable - Slubbix - 25.06.2013 15:21

Klassiker: Mit den richtigen [Link: Registrierung erforderlich] wird es wahrscheinlich funktionieren.

PHP-Code:
protected function getCurrentSection() {
    if (
$this->current_section === 'thead') {
        return 
$this->thead;
    } elseif (
$this->current_section === 'tfoot') {
        return 
$this->tfoot;
    } else {
        return 
$this->tbody_array[$this->current_section];
    }


Zudem ist das Aufrufen von Operationen als Referenz veraltet:

PHP-Code:
$ref = &$this->getCurrentSection(); 




RE: [Help] PHP OOP - Weitergabe einer Variable - Sysix - 25.06.2013 18:08

1. ich will ja nicht abfragen ob das null ist

habs aber trotzdem mal gemacht Wink

2. mit und ohne Referenz-Zeichen funzt es net :/


RE: [Help] PHP OOP - Weitergabe einer Variable - sic_ - 25.06.2013 22:16

Wo liegt überhaupt das unerwünschte Verhalten? Erstell uns doch mal einen Testfall: Mit welchem Argumenten muss man addSection() aufrufen, damit das Problem auftritt?

Ist dein Problem, dass (wenn die Klasse "Test" heißt)

Code:
$test = new Test();
$test->addSection('thead', 'Testclass');

nicht $thead['attr']['class'] === 'thead' ist?

- Dann kapsel halt die Arrays für die einzelnen Tabellen wieder in einem Array...
- Oder arbeite direkt mit 3 verschiedenen Methoden zum Adden.
- Oder baue dir eben noch eine setCurrentSection()-Methode mit der eine Section invers zu getCurrentSection() wieder gespeichert wird

Code:
$section = $this->getCurrentSection();
        
         if( ! empty($class))
             $attributes['class'] = $class;
         $section['attr'] = $attributes;
         $section['rows'] = array();
        
         setCurrentSection($section);
     }
    
     protected function setCurrentSection($section) {
         if ($this->current_section == 'thead') {
             $this->thead = $section;    
         } elseif($this->current_section == 'tfoot') {
             $this->tfoot = $section;  
         } else { // tbody
             $this->tbody_array[$this->current_section] = $section;    
         }
     }




RE: [Help] PHP OOP - Weitergabe einer Variable - Sysix - 26.06.2013 08:29

danke mit setCurrentSection hats geklappt Biggrin

was rausgenommen wird muss ja wieder gespeichert werden .___.
ich war da vollkommnt auf das Referenz-Zeichen fixiert.

für deine Frage ein $var->addSection('tbody'); würde scho reichen.. der Rest sind einfach nur Attribute wie style="", id="", align="" und so nen zeug Smile


RE: [Help] PHP OOP - Weitergabe einer Variable - sic_ - 26.06.2013 10:11

Ok sehr schön Smile