Visualizzazione dei risultati da 1 a 6 su 6

Discussione: [oop] teoria

  1. #1

    [oop] teoria

    Salve ragazzi...

    allora supponiamo che io ho 2 classi indipendenti tra loro.. class utenti e class controllo..

    Alla class utenti passo come valori il nickname e la password

    $utente = new utenti($nick, $pass);

    Io però voglio che all'interno della classe utenti venga eseguito un metodo della classe controllo proprio x controllate i dati inseriti.. come si fà a richiamare il metodo che è di un'altra classe???

    Azieeeeee
    Questa volta, più che un voto.. è favoreggiamento.

  2. #2

    .............

    Ciao.
    Si chiama aggregation e con
    PHP5 c'è pure l'utile Type Hinting esempio per PHP5:
    Codice PHP:
      <?php

    class Question  {
        private 
    $marker;
        public 
    $prompt "";
        public 
    $score 0;
        public 
    $response "";

        function 
    __construct$prompt_sMarker $marker ) {
            
    $this->prompt $prompt_s;
            
    $this->marker $marker;
        }

        function 
    answer$response_s ) {
            
    $this->score 0;
            
    $this->response $response_s;
            if ( 
    $this->marker->mark$response_s ) ) {
                
    $this->score 1;
            }
        }
    }

    class 
    Marker {
        protected 
    $condition;
        function 
    __construct$condition_s ) {
            
    $this->condition $condition_s;
        }
       
        function 
    mark$response_s ) {
            return ( 
    $this->condition == $response_s );
        }
    }
    $q = new Question"how many beans make 5",
                        new 
    Marker'five' ) );
    $responses = array( "five""six" );

    foreach ( 
    $responses as $response ) {
        
    $q->answer$response );
        print 
    "response: $response scores {$q->score}\n";

    // output:
    // response: five scores 1
    // response: six scores 0
    ?>


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

  3. #3
    .............

    Non penso di poter ancora utilizzare PHP 5.. x la versione 4 com'è la storia???
    Questa volta, più che un voto.. è favoreggiamento.

  4. #4

    .............

    Qui
    trovi un esempio in PHP4 ad ogni modo la cosa non cambia
    di molto come parametro passi l'oggetto al limite con un controllo
    del genere:
    Codice PHP:
    function __construct$prompt_s$marker ) {
        if ( ! ( 
    $marker instanceof Marker ) ) {
            die( 
    "Expected Marker object" );   
        } 
    invece di __construct usi il nome della classe
    e la posto dei vari public private (modificatori di accesso) usi la solita
    var.
    In questo modo (possono esserci degli errori non posso
    testarlo)


    Codice PHP:
    <?php

    class Question  {
        var 
    $marker;
        var 
    $prompt "";
        var 
    $score 0;
        var 
    $response "";

        function 
    Question$prompt_s$marker ) {
            
    $this->prompt $prompt_s;
            
    $this->marker $marker;
        }

        function 
    answer$response_s ) {
            
    $this->score 0;
            
    $this->response $response_s;
            if ( 
    $this->marker->mark$response_s ) ) {
                
    $this->score 1;
            }
        }
    }

    class 
    Marker {
        var 
    $condition;
        function 
    Marker$condition_s ) {
            
    $this->condition $condition_s;
        }
       
        function 
    mark$response_s ) {
            return ( 
    $this->condition == $response_s );
        }
    }
    $q = new Question"how many beans make 5",
                        new 
    Marker'five' ) );
    $responses = array( "five""six" );

    foreach ( 
    $responses as $response ) {
        
    $q->answer$response );
        print 
    "response: $response scores {$q->score}\n";
    }
    Without faith, nothing is possible. With it, nothing is impossible
    http://ilwebdifabio.it

  5. #5
    ok allora 2 cose..


    1: $this->marker->mark( $response_s ) <--non capisco...

    2: {$q->score} <---non ricordo a cosa servono le {} e xke richiami $q->score?? A COSA SI RIFERISCE???

    Grazie Whisher.. mi prenoto x un corso da te...
    Questa volta, più che un voto.. è favoreggiamento.

  6. #6

    ...........

    1: $this->marker->mark( $response_s ) <--non capisco...
    Passando un oggetto come parametro l'oggetto di
    riferimento acquista tutte le proprietà e i metodi
    di quel oggetto che quindi puoi richiamare.


    {$q->score} <---non ricordo a cosa servono le {} e xke richiami $q->score?? A COSA SI RIFERISCE???
    Score è una proprietà della classe Question.
    per le graffe dai un occhio qui


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

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.