Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 15
  1. #1
    Utente di HTML.it L'avatar di brodik
    Registrato dal
    Jan 2009
    Messaggi
    765

    variabile globale nelle classi

    Allora ragazzi intanto buongiorno, anzi buonasera a tutti =)

    Il problema è il seguente...

    Ho una classe (e fin qui tutto ok ), quando la inizializzo in questo modo funziona correttamente (quella che posto è solo la porzione che mi crea problemi, l'ho semplificata per togliere le parti inutili che non mi/ci interessano, quindi parti come $this->parse = "thearray"; in realtà provengono da altre funzioni...):

    Codice PHP:
    <?php

        $thearray
    [1][id]    =    "1";
        
    $thearray[1][foo]    =    "primo";
        
    $thearray[2][id]    =    "2";
        
    $thearray[2][foo]    =    "secondo";
        
    $thearray[3][id]    =    "3";
        
    $thearray[3][foo]    =    "terzo";
        
    $thearray[4][id]    =    "4";
        
    $thearray[4][foo]    =    "quarto";
        
    $thearray[5][id]    =    "5";
        
    $thearray[5][foo]    =    "quinto";

        
    $idx = new parsing;

        
    $idx->pa();

    class 
    parsing{

        function 
    pa(){

            
    $this->parse "thearray";

            
    $newstyle $this->parse;
                
            global $
    $newstyle;

            
    $this->newstyle = $$newstyle;

    print_r($this->newstyle);

            return 
    $this->newstyle;

        }

    }

    ?>
    restituisce

    codice:
    Array ( [1] => Array ( [id] => 1 [foo] => primo ) [2] => Array ( [id] => 2 [foo] => secondo ) [3] => Array ( [id] => 3 [foo] => terzo ) [4] => Array ( [id] => 4 [foo] => quarto ) [5] => Array ( [id] => 5 [foo] => quinto ) )
    se invece provo così, cioè richiamando la classe da un'altra classe, mi restituisce NULL (con il var_dump al posto di print_r):

    Codice PHP:
    <?php

    $idxxx 
    = new myf;

    $idxxx->myfunc();

    class 
    myf{

        function 
    myfunc(){

            
    $thearray[1][id]    = "1";
            
    $thearray[1][foo]    = "primo";
            
    $thearray[2][id]    = "2";
            
    $thearray[2][foo]    = "secondo";
            
    $thearray[3][id]    = "3";
            
    $thearray[3][foo]    = "terzo";
            
    $thearray[4][id]    = "4";
            
    $thearray[4][foo]    = "quarto";
            
    $thearray[5][id]    = "5";
            
    $thearray[5][foo]    = "quinto";

            
    $idx = new parsing;

            
    $idx->pa();

        }
    }

    class 
    parsing{

        function 
    pa(){

            
    $this->parse "thearray";

            
    $newstyle $this->parse;
                
            global $
    $newstyle;

            
    $this->newstyle = $$newstyle;

    print_r($this->newstyle);

            return 
    $this->newstyle;

        }

    }

    ?>
    ecco... dove sta il problema?

    spero che riusciate ad aiutarmi..

  2. #2

    Re: variabile globale nelle classi

    Originariamente inviato da brodik
    ecco... dove sta il problema?
    E' un problema di scope. Vedi:

    http://www.php.net/manual/en/languag...bles.scope.php

    Tu tenti di importare nello scope locale la variabile $thearray che però NON è una variabile globale.

    Non staresti di meno ad usare una cosa tipi $this->thearray etc. piuttosto che appoggiarti alle molto poco OO variabili globali?

  3. #3
    Utente di HTML.it L'avatar di brodik
    Registrato dal
    Jan 2009
    Messaggi
    765
    cercando una soluzione infatti avevo intuito che il problema è dovuto alla variabile globale..

    non potresti postare una soluzione possibile in modo da togliere anche il global $$newstyle; ?

    è ormai 4 giorni che sto sopra a questo problema

  4. #4

    Re: variabile globale nelle classi

    Molto probabilmente non ho capito cosa vuoi fare, xò non sarebbe molto più semplice:

    Codice PHP:
    $idxxx = new myf;
    $idxxx->myfunc();

    class 
    myf{

        function 
    myfunc(){
            
    $thearray[1][id]    = "1";
            
    $thearray[1][foo]    = "primo";
            
    $thearray[2][id]    = "2";
            
    $thearray[2][foo]    = "secondo";
            
    $thearray[3][id]    = "3";
            
    $thearray[3][foo]    = "terzo";

            
    $idx = new parsing;

            
    $idx->pa($thearray);

        }
    }

    class 
    parsing{

        function 
    pa($thearray){

            
    $newstyle $thearray;

    print_r($newstyle);

            return 
    $newstyle;
        }
    }

    ?> 
    Administrator of NAMDesign.Net

  5. #5
    Utente di HTML.it L'avatar di brodik
    Registrato dal
    Jan 2009
    Messaggi
    765
    mi restituisce sempre NULL

    comunque mi serve che la funzione "pa()" mi restituisca come risultato

    codice:
    Array ( [1] => Array ( [id] => 1 [foo] => primo ) [2] => Array ( [id] => 2 [foo] => secondo ) [3] => Array ( [id] => 3 [foo] => terzo ) [4] => Array ( [id] => 4 [foo] => quarto ) [5] => Array ( [id] => 5 [foo] => quinto ) )
    che poi andrà in pasto ad altre funzioni della classe...

    Altre soluzioni?

  6. #6
    sostituisci:
    $this->newstyle
    con
    $newstyle

    sia nel print_r() che nel return
    Administrator of NAMDesign.Net

  7. #7
    Utente di HTML.it L'avatar di brodik
    Registrato dal
    Jan 2009
    Messaggi
    765
    perfetto funziona

    ora però vediamo un po' se posso ottimizzarlo..

    Codice PHP:
    $idxxx = new myf;
    $idxxx->myfunc();

    class 
    myf{

        function 
    myfunc(){
            
    $thearray[1][id]    = "1";
            
    $thearray[1][foo]    = "primo";
            
    $thearray[2][id]    = "2";
            
    $thearray[2][foo]    = "secondo";
            
    $thearray[3][id]    = "3";
            
    $thearray[3][foo]    = "terzo"
    fino a qui tutto perfetto

    Codice PHP:

            $idx 
    = new parsing;

            
    $idx->pa($thearray);

        }

    io passo la variabile "thearray" (questo perchè passo alla funzione una stringa di testo contenente "thearray" e con una regex estrapolo il singolo "thearray" (ESEMPIO: "sort thearray desc on id") e tralascio tutte le operazioni per estrarlo) quindi sarebbe

    Codice PHP:
    $idx->pa(thearray); 
    Quindi io avrei scritto questo con il suggerimento di LeaderGL:

    Codice PHP:
    <?php

    $idxxx 
    = new myf;

    $idxxx->myfunc();

    class 
    myf{

        function 
    myfunc(){
            global 
    $thearray;

            
    $thearray[1][id]    = "1";
            
    $thearray[1][foo]    = "primo";
            
    $thearray[2][id]    = "2";
            
    $thearray[2][foo]    = "secondo";
            
    $thearray[3][id]    = "3";
            
    $thearray[3][foo]    = "terzo";

            
    $idx = new parsing;

            
    $idx->pa(thearray);

        }
    }

    class 
    parsing{

        function 
    pa($parse){
            global 
    $thearray;

            
    $newstyle $parse;

    print_r($$newstyle);

            return $
    $newstyle;
        }


    ?>
    tutto funziona che è una meraviglia

    però non c'è un modo per togliere le global?? perchè se per ogni funzione in cui uso la classe parsing devo aggiungere global $thearray; diventa un po' macchinoso....

  8. #8
    Utente di HTML.it L'avatar di brodik
    Registrato dal
    Jan 2009
    Messaggi
    765
    un aiutino?

  9. #9
    Utente di HTML.it L'avatar di brodik
    Registrato dal
    Jan 2009
    Messaggi
    765
    aiutino...?

  10. #10
    xkè hai aggiunto i global? quel codice che ti avevo scritto andava bene così com'era.

    se tu alla funzione passi un argomento è inutile che questo stesso argomento (i suoi dati) li gestisci anche attraverso global...
    Administrator of NAMDesign.Net

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.