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

    $_REQUEST a cosa serve?

    Ho analizzato un codice php con questa classe:

    Codice PHP:
    class CommandContext {
        private 
    $params = array();
        private 
    $error "";
        function 
    __construct() {
            
    $this->params $_REQUEST;
        }
        function 
    addParam$key$val ) { 
            
    $this->params[$key]=$val;
        }
        function 
    get$key ) { 
            if ( isset( 
    $this->params[$key] ) ) {
                return 
    $this->params[$key];
            }
            return 
    null;
        }
        function 
    setError$error ) {
            
    $this->error $error;
        }
        function 
    getError() {
            return 
    $this->error;
        }
        function 
    ottieni_parametri(){
            return 
    $this->params;
        }

    sapreste dirmi a che serve questo $_REQUEST? Se elimino la riga contenente questo array associativo tutto lo script funziona ugualmente bene.

    Ciao
    Più pratica in futuro...

  2. #2
    Utente di HTML.it L'avatar di clasku
    Registrato dal
    Aug 2006
    Messaggi
    3,197
    cioè, se togli i riferimenti a $_REQUEST la funzione get passando un parametro funziona? mi pare un pelo strano

  3. #3
    listing11.12.php
    Codice PHP:
    <?php
    class CommandNotFoundException extends Exception {}
    class 
    CommandFactory {
        private static 
    $dir 'commands';
        static function 
    getCommand$action='Default' ) {
            if ( 
    preg_match'/\W/'$action ) ) {
                throw new 
    Exception("illegal characters in action");
            }
            
    $class UCFirst(strtolower($action))."Command";  
            
    $file self::$dir.DIRECTORY_SEPARATOR."$class.php";
            if ( ! 
    file_exists$file ) ) {
                throw new 
    CommandNotFoundException"could not find '$file'" );
            }
            require_once( 
    $file );
            if ( ! 
    class_exists$class ) ) {
                throw new 
    CommandNotFoundException"no '$class' class located" );
            }
            
    $cmd = new $class();
            return 
    $cmd;
        }
    }
    class 
    Controller {
        private 
    $context;
        function 
    __construct() {
            
    $this->context = new CommandContext();
        }
        function 
    getContext() {
            return 
    $this->context;
        }
        function 
    process() {
            
    $action $this->context->get('action');
            
    $action = ( is_null$action ) ) ? "default" $action;
            
    $cmd CommandFactory::getCommand$action );
            if ( ! 
    $cmd->execute$this->context ) ) {
                
    // handle failure
            
    } else {
                
    // success
                // dispatch view
            
    }
        } 
    }    
    // ------------- helper stuff
    class User{
        private 
    $name;
        function 
    __construct$name ) {
            
    $this->name $name;
        }
    }
    class 
    Registry {
        static function 
    getMessageSystem() {
            return new 
    MessageSystem();
        }
        static function 
    getAccessManager() {
            return new 
    AccessManager();
        }
    }
    class 
    MessageSystem {
        function 
    send$mail$msg$topic ) {
            print 
    "sending $mail$topic$msg\n";
            return 
    true;
        }
        function 
    getError() {
            return 
    "move along now, nothing to see here";
        }
    }
    class 
    AccessManager {
        function 
    login$user$pass ) {
            
    $ret = new User$user );
            return 
    $ret;
        }
        function 
    getError() {
            return 
    "move along now, nothing to see here";
        }
    }
    class 
    CommandContext {
        private 
    $params = array();
        private 
    $error "";
        function 
    __construct() {
            
    $this->params $_REQUEST;
        }
        function 
    addParam$key$val ) { 
            
    $this->params[$key]=$val;
        }
        function 
    get$key ) { 
            if ( isset( 
    $this->params[$key] ) ) {
                return 
    $this->params[$key];
            }
            return 
    null;
        }
        function 
    setError$error ) {
            
    $this->error $error;
        }
        function 
    getError() {
            return 
    $this->error;
        }
        function 
    ottieni_parametri(){
            return 
    $this->params;
        }
    }
    $controller = new Controller();
    $context $controller->getContext();
    $context->addParam('action''feedback' );
    $context->addParam('email''bob@bob.com' );
    $context->addParam('topic''my brain' );
    $context->addParam('msg''all about my brain' );
    $controller->process();
    print 
    $context->getError()."<br>";
    print_r($context->ottieni_parametri());
    ?>
    commands/Command.php
    Codice PHP:
    <?php
    abstract class Command {
        abstract function 
    executeCommandContext $context );
    }
    ?>
    commands/DefaultCommand.php
    Codice PHP:
    <?php
    require_once( "Command.php" );

    class 
    DefaultCommand extends Command {

        function 
    executeCommandContext $context ) {
            
    // default command
            
    return true;
        }
    }
    ?>
    commands/FeedbackCommand.php
    Codice PHP:
    <?php
    require_once( "Command.php" );

    class 
    FeedbackCommand extends Command {

        function 
    executeCommandContext $context ) {
            
    $msgSystem Registry::getMessageSystem();
            
    $email $context->get'email' );
            
    $msg   $context->get'msg' );
            
    $topic $context->get'topic' );
            
    $result $msgSystem->send$email$msg$topic );
            if ( ! 
    $result ) {
                
    $context->setError$msgSystem->getError() );
                return 
    false;
            }
            return 
    true;
        }
    }


    ?>
    commands/LoginCommand.php
    Codice PHP:
    <?php
    require_once( "Command.php" );

    class 
    LoginCommand extends Command {

        function 
    executeCommandContext $context ) {
            
    $manager Registry::getAccessManager();
            
    $user $context->get'username' );
            
    $pass $context->get'pass' );
            
    $user_obj $manager->login$user$pass );
            if ( 
    is_null$user_obj ) ) {
                
    $context->setError$manager->getError() );
                return 
    false;
            }
            
    $context->addParam"user"$user_obj );
            return 
    true;
        }
    }
    ?>
    Più pratica in futuro...

  4. #4
    A me sembra che $this->params = $_REQUEST; sia ben presente.
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

  5. #5
    Quote Originariamente inviata da satifal Visualizza il messaggio
    A me sembra che $this->params = $_REQUEST; sia ben presente.
    però se lo rimuovi il codice va uguale...
    Più pratica in futuro...

  6. #6
    Utente di HTML.it L'avatar di m4rko80
    Registrato dal
    Aug 2008
    residenza
    Milano
    Messaggi
    2,655
    Semplicemente presumo che se inizializzi la classe su di una chiamata in GET o POST automaticamente avrai $this->params come array con chiavi e valori derivanti appunto dai parametri passati.
    In alro caso sarà comunque un array vuoto o con qualche valore.
    In $_REQUEST c'è il contenuto sia di GET che POST

  7. #7
    Utente di HTML.it L'avatar di luca200
    Registrato dal
    Apr 2002
    Messaggi
    4,120
    Quote Originariamente inviata da giannino1995 Visualizza il messaggio
    però se lo rimuovi il codice va uguale...
    Dipende cosa ci fai col codice!
    Finché i parametri ce li metti tu "a mano", come mi par di vedere, è ovvio che poi li ritrovi. Ma chi ha pensato la classe intendeva metterti a disposizione i parametri GET e POST, e se togli quell'istruzione chiaramente non li hai più

  8. #8
    Quote Originariamente inviata da giannino1995 Visualizza il messaggio
    però se lo rimuovi il codice va uguale...
    Non va uguale...ti ritrovi i parametri che hai inserito tu ma non quelli presenti $_REQUEST che andranno persi.
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

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 © 2026 vBulletin Solutions, Inc. All rights reserved.