Visualizzazione dei risultati da 1 a 9 su 9
  1. #1

    Problema con variabile di tipo array di una classe

    Salve,
    ho una classe dove una variabile è un array. Però l'array lo inizializzo in un secondo momento. Praticamente non mi viene creata la struttura dell'array in modo corretto. Forse uso male gli array (sono abituato ad altri linguaggi).

    1. Come inizializzo una variabile che sarà un array? Io ho fatto $users = array() e dopo lo inizializzo. E' corretto così?

    2. Per aggiungere un elemento singolarmente ogni volta basta che faccio $this -> array[] = "Luca" o devo usare un altro metodo? E se passo un array come parametro ad un metodo interno alla classe basta che faccio $this -> users = $utenti; dove $utenti è già un array creato con array("Admin", "Luca") per esempio

    Grazie mille!

    Luca
    Prima di dire che sei un Webmaster, guarda in giro per la rete....

  2. #2
    Esempio:
    Codice PHP:
    <?php 
    class A{
        private 
    $users= array();
        public function 
    __construct(){}
        public function 
    setUser($key,$value){
            
    $this->users[$key]= $value;
        }
        public function 
    getUser($key){
            return 
    $this->users[$key];
        }
               public function 
    getUsers(){
            return 
    $this->users;
        }
        public function 
    has($key){
            return isset(
    $this->users[$key]);
        }
    ?>
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  3. #3
    Mi sembra di capire che la sintassi sia riferita al PHP5, se non erro. Io invece devo usare il PHP4.
    Cambia qualcosa nella sintassi?
    Comunque il concetto l'ho afferrato.. proverò a fare così.

    Grazie!
    Prima di dire che sei un Webmaster, guarda in giro per la rete....

  4. #4
    Originariamente inviato da Luca450Mhz
    Mi sembra di capire che la sintassi sia riferita al PHP5, se non erro. Io invece devo usare il PHP4.
    Cambia qualcosa nella sintassi?
    Comunque il concetto l'ho afferrato.. proverò a fare così.

    Grazie!
    al posto di
    private $users= array();
    php4
    var $users= array();

    i metodi senza la visibilità

    function getUser($key){
    return $this->users[$key];
    }

    a il costruttore con il nome della classe.
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  5. #5
    Infatti..
    Il problema mio è leggermente diverso in quanto la variabile è un array di un'altra classe da me definita. Devo gestire una specie di grafico, dove ci sono dei nodi (classe Nodo) collegati tra di loro con next e prev. Il problema è che ci possono essere più prev e più next nel mio caso.
    Io creo un array di Node e poi imposto con un metodo setNext il successore di ogni nodo con un elemento dell'array. Non vorrei fosse questo il problema (perchè non mi vengono memorizzati).

    Codice PHP:
    $this -> nodes[0] -> setType("INIT");
    $this -> nodes[0] -> setPrev(null);
    $this -> nodes[0] -> setNext($this -> nodes[1]);
    $this -> nodes[0] -> setUsers(array("Admin""Luca"));
    $this -> nodes[1] -> setType("NORMAL");
    $this -> nodes[1] -> setPrev($this -> nodes[0]);
    $this -> nodes[1] -> setNext($this -> nodes[2]);
    $this -> nodes[3] -> setType("FINISH");
    $this -> nodes[3] -> setPrev($this -> nodes[2]);
    $this -> nodes[3] -> setNext(null);
    $this -> nodes[3] -> setUsers(array("Admin""Luca")); 
    Non so se è scorretto a questo punto il modo di passare le altre strutture Node per legarle conme next e prev..

    Il metodo per settare il next per esempio è il seguente:

    Codice PHP:
    function setNext($nodeNext) {
            if (
    $nodeNext != null) {
                if (
    is_a($nodeNext"Node"))  {
                    
    $this -> node_next[] = $nodeNext;
                } else {
                    echo 
    "Errore. Parametro non di tipo \"Node\" ma di tipo: " gettype($nodeNext) . "
    "
    ;
                }
            }
            
        } 
    Prima di dire che sei un Webmaster, guarda in giro per la rete....

  6. #6
    Preciso che l'array di Node viene creato tutto prima con:

    Codice PHP:
    for ($i 0$i numNodi$i++) {
         
    $this -> nodes[$i] = new Node("Nome" $i"Tipo");

    Prima di dire che sei un Webmaster, guarda in giro per la rete....

  7. #7
    Sono di furia devo scappare
    ma però ad occhio questo
    Codice PHP:
    function setNext($nodeNext) { 
            if (
    $nodeNext != null) { 
                if (
    is_a($nodeNext"Node"))  { 
                    
    $this -> node_next[] = $nodeNext
                } else { 
                    echo 
    "Errore. Parametro non di tipo \"Node\" ma di tipo: " gettype($nodeNext) . "
    "

                } 
            } 
             
        } 
    mi torna poco se setNext è un metodo di Node
    questi due if
    if ($nodeNext != null) {
    if (is_a($nodeNext, "Node"))

    c'entrano poco
    a pro is_a è deprecato usa instanceof
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  8. #8
    Codice PHP:
    $this -> nodes[0] -> 
    anche questo è sbagliato ad occhio
    non è un oggetto

    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  9. #9
    Grazie mille che nonostante eri di fretta mi hai aiutato!
    Lo so che è deprecato, ma instanceof esiste solo sul PHP5, altrimenti lo avrei usato.. cmq vedo se il problema potrebbe stare in quegli if..

    Ciao!
    Prima di dire che sei un Webmaster, guarda in giro per la rete....

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.