Visualizzazione dei risultati da 1 a 4 su 4
  1. #1
    Utente di HTML.it L'avatar di Stamos
    Registrato dal
    Jul 2004
    Messaggi
    160

    [php]Gestione degli errori durante la creazione di un oggetto

    Sto cercando di fare un costruttore per una classe ChatRoom, che crea degli oggetti chatroom(mi serve per svilupare una chat). Lo faccio all'interno di un progetto(sto faccendo uno stage), il che vuol dire mi devo adattare alla mentalità in cui è stato fatto il resto del progetto ...

    Dunque nel mio costruttore devo,ovviamente, gestire anche i casi dove ci sono dei problemi, come per esempio la creazione fallita di una nuova chatroom.

    Visto la mia esperienza minima(solo 20 giorni che lavoro con il php)io farrei una "print" dando un msg di errore ...

    Ma mi hanno detto che nel progetto intero, non esistono le print, e che devo gestire gli errori, tramite le proprietà del oggetto ...

    Cosa che non so fare ... ma neanche ho capito cosa intendono ...

    Visto che sono bloccato e non so come andare avanti ... vi riporto qui sotto il pezzo di codice che sono riuscito a scrivere fino addesso ... se qualcuno ha 5' e puo' dare una occhiata dicendomi come posso fare ... lo ringrazio in anticipo.

    var $id_chatroom = 0;
    var $id_course_instance = 0;
    var $chat_type = "";
    var $chat_moderator = "";
    var $chat_title = "";
    var $chat_topic = "";
    var $max_users= 25;
    var $welcome_msg = "";
    var $start_time = 0;
    var $end_time = 0;
    var $error = "";

    //main constructor function of the class ChatRoom;
    function ChatRoom($id_chatroom)
    {
    //we check if the id_chatroom it is provided;
    if($id_chatroom)
    {
    //if it is provided, we check if the chatroom allready exists;
    $chatroom_ha = $this->get_chatroomFN($id_chatroom);
    if ($chatroom_ha)
    {
    // the chatroom allready exists;
    $this->chat_type = $chatroom_ha['chat_type'];
    $this->chat_moderator = $chatroom_ha['chat_moderator'];
    $this->chat_title = $chatroom_ha['chat_title'];
    $this->chat_topic = $chatroom_ha['chat_topic'];
    $this->max_users = $chatroom_ha['max_users'];
    $this->welcome_msg = $chatroom_ha['welcome_msg'];
    $this->start_time = $chatroom_ha['start_time'];
    $this->end_time = $chatroom_ha['end_time'];
    $this->id_course_instance = $chatroom_ha['id_course_instance'];
    $this->id_chatroom = $chatroom_ha['id_chatroom'];

    le proprietà qui sopra riportate sono anche all'interno della tabella chatroom nello DB,

    devo inserire anche la proprietà error nello DB?
    gli devo assegnare un valore qui sopra?
    }
    else
    {
    //error, the chatroom with such id_chatroom does not exist!
    //so we proceed by creating a new chatroom;
    $result_id = $this->add_chatroomFN();
    if ($result_id)
    {
    //a new chatroom has succefully been created
    $this->id_chatroom = $result_id;
    }
    else
    {
    //error the chatroom has not been created
    $this->error =
    }
    }
    }

  2. #2
    Utente di HTML.it L'avatar di Stamos
    Registrato dal
    Jul 2004
    Messaggi
    160
    aiuto

  3. #3
    Ciao,
    la gestione degli errori nel costruttore in PHP4 richiede qualche "giro di codice" (il costruttore non può restituire nulla) mentre in PHP5 puoi utilizzare le eccezioni

    Esempio php4

    class Ciccio
    {

    var $error = '' ;

    function Ciccio()
    {
    //codice

    if(...CONDIZIONE ERRORE...)
    {
    $this->addError('bla bla bla') ;
    }
    }

    function addError($msg)
    {
    $this->error = $msg ;
    }

    function hasError()
    {
    return(!empty($this->error)) ;
    }

    }

    $x = new Ciccio() ;

    if($x->hasError())
    {
    //gestisci errore
    }
    per favore NIENTE PVT TECNICI da sconosciuti

  4. #4
    Utente di HTML.it L'avatar di Stamos
    Registrato dal
    Jul 2004
    Messaggi
    160
    mille grazie!

    provo subito!

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.