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

    Ereditarietà del tipi di argomento nel metodi

    Ciao a tutti, volevo sapere come posso fare per realizzare una cosa del genere:
    Codice PHP:

    //Tipi degli oggetti
    class SuperType {}
    class 
    Type extends SuperType {}


    class 
    Foo {
        public function 
    test(SuperType $t) {
            echo 
    get_class($t);
        }
    }

    class 
    Bar {
        public function 
    test(Type $t) {
            echo 
    get_class($t);
        }

    In questo caso ho un errore del tipo:
    Fatal error: Declaration of Bar::test() must be compatible with that of Foo::test()
    Cioè vorrei fare una specie di overloading sul metodo test che in php non è previsto, quindi nel metodo Bar::test() devo usare per forza SuperType come tipo o ci sono altre soluzioni?
    Coltiva Linux, Windows si pianta da solo!

  2. #2
    Forse non ho capito il problema:

    Codice PHP:
    <pre>
    <?php 

    class SuperType {}

    class 
    Type extends SuperType {}

    class 
    Foo {
        public function 
    test(SuperType $t) {
            echo 
    get_class($t) . "\r\n";
        }
    }

    class 
    Bar {
        public function 
    test(Type $t) {
            echo 
    get_class($t) . "\r\n";
        }
    }

    $s = new SuperType();
    $t = new Type();

    $b = new Bar();
    $b->test($t);

    $f = new Foo();
    $f->test($s);
    $f->test($t);

    ?>
    </pre>

  3. #3
    Uso solamente new Bar() in modo da poter fare una cosa del tipo:
    Codice PHP:
    $s = new SuperType();
    $t = new Type();

    $b = new Bar();

    //Questo dovrebbe dare errore perchè Bar accetta solo Type e non SuperType
    $b->test($s);

    //Qui invece va tutto bene perchè $t è di tipo Type
    $b->test($t); 
    Coltiva Linux, Windows si pianta da solo!

  4. #4
    Forse non mi sono spiegato bene, io vorrei usare il type hint in questo modo:
    Codice PHP:
    class SuperType {}
    class 
    Type extends SuperType {}


    class 
    Foo {
        static public function 
    test(SuperType $t) {
            echo 
    get_class($t);
        }
    }

    //Questo non mi viene accettato perché si aspetta un SuperType e non un Type
    Foo::test(new Type()); 
    Mi da errore ma non capisco perchè, Type è un SuperType e quindi ha tutte le proprietà di un SuperType
    Coltiva Linux, Windows si pianta da solo!

  5. #5
    up
    Coltiva Linux, Windows si pianta da solo!

  6. #6
    a me questo ultimo codice che hai postato funziona... ma te che versione di php usi?

  7. #7
    Ops, ho postato il codice sbagliato
    Quello che non va e reso un po' più semplice è così:
    Codice PHP:

    class Foo {
        public 
    $val;
        public function 
    test(Foo $t) {
            return (
    $this->val == $t->val);
        }
    }


    class 
    Bar extends Foo {
        public 
    $barVal;
        
    //Questo non me lo accetta
        
    public function test(Bar $t) {
            return (
    parent::test()) && ($this->barVal == $t->barVal);
        }
    }

    //$f1 e $f2 di tipo Foo
    $f1 = new Foo();
    $f2 = new Foo();
    $f1->test($f2);

    //$b1 e $b2 di tipo Bar
    $b1 = new Bar();
    $b2 = new Bar();
    $b1->test($b2);

    //$f1 di tipo Foo e $b1 di tipo Bar
    $f1->test($b1); 
    In pratica non posso fare l'override del metodo test.
    Il metodo test di Bar si aspetta un altro oggetto Bar come input perché in Foo non esiste la variabile $barVal.

    Edit: corretto il codice
    Coltiva Linux, Windows si pianta da solo!

  8. #8

  9. #9
    Ho risolto:
    Codice PHP:
    class Foo {
        public 
    $val;
        public function 
    test(Foo $t) {
            return (
    $this->val == $t->val);
        }
    }

    class 
    Bar extends Foo {
        public 
    $barVal;
        
    //Questo non me lo accetta
        
    public function test(Bar $t) {
            return (
    parent::test($t)) && ($this->barVal == $t->barVal);
        }
    }

    //$f1 e $f2 di tipo Foo
    $f1 = new Foo();
    $f2 = new Foo();

    $f1->val=1;
    $f2->val=1;

    var_dump($f1->test($f2)); //$f1->val == $f2->val : true

    //$b1 e $b2 di tipo Bar
    $b1 = new Bar();
    $b2 = new Bar();
    $b1->val=1;
    $b1->barVal=2;
    $b2->val=2;
    $b2->barVal=2;

    var_dump($b1->test($b2)); //($b1->val == $b2->val) && ($b1->barVal == $b2->barVal) : (false && true) -> false

    //$f1 di tipo Foo e $b1 di tipo Bar
    var_dump($f1->test($b1)); //$f1->val == $b1->val : true 
    Coltiva Linux, Windows si pianta da solo!

  10. #10
    Il problema che avevo era dato dal fatto che non usavo l'estensione di una classe ma implementavo una interfaccia.
    Codice PHP:
    interface Foo {
        public function 
    test(Foo $t);
    }

    class 
    Bar implements Foo {
        
    //Questo non me lo accetta
        
    public function test(Bar $t) {
            return (
    parent::test($t)) && ($this->barVal == $t->barVal);
        }

    Invece di un interfaccia utilizzo una classe astratta con implementazione del metodo test vuota, non so se va bene.
    Coltiva Linux, Windows si pianta da solo!

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