Visualizzazione dei risultati da 1 a 7 su 7

Discussione: [PHP5]OOP in PHP

  1. #1
    Utente di HTML.it
    Registrato dal
    Feb 2005
    Messaggi
    209

    [PHP5]OOP in PHP

    E' possibile in PHP5 fare sì che un istanza di una classe richiamata senza un metodo o una proprietà sia una della sue proprietà?

    Ad esempio:

    Codice PHP:
    class A{
        public 
    $a;
        public function 
    __construnct($a){
            
    $this->a=$a;
        }
        
        function 
    __toString(){
            return 
    $this->a;
        }
    }

    $a=new A(array(0,1,2,3,4,5));

    echo 
    $a[1]; // 1 
    La cosa più vicina per fare questa cosa è il magic method __toString, ma naturalmente non fa quello che ho in mente.

    Grazie ciao!

  2. #2
    Moderatore di Server Apache L'avatar di marketto
    Registrato dal
    Sep 2001
    Messaggi
    5,858
    codice:
    class A{
        public $a;
        public function __construct($a){
            $this->a = $a;
        }
        
        function __toString(){
            return serialize( $this->a );
        }
    }
    
    $a=new A(array(0,1,2,3,4,5));
    
    $t = unserialize( $a );
    echo $t[1];
    think simple think ringo

  3. #3
    Dai anche un occhio
    a overloading

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

  4. #4
    Utente di HTML.it
    Registrato dal
    Feb 2005
    Messaggi
    209
    potrei usare __call, ma se volessi evitare di usare il richiamo di un metodo inesistente, potrei anche fare un metodo che contiene return $this->a;

    Vorrei ricreare il comportamento che hanno per esempio le stringhe (negli altri linguaggi OOP):

    Codice PHP:
    $s= new String('ciao');
    echo 
    $s//ciao
    echo $s->first_letter(); //c
    echo $s[0]; //c 
    Se si può fare con __call illustrami come.
    Vorrei evitare di usare metodi perchè sto riscrivendo una classe con un metodo che ritornava un array(), ma sviluppandola ora torna l'istanza di un'altra classe, che per retrocompatibilità non va più bene, almeno che l'istanza senza metodi non possa essere una sua proprietà, va bene anche se non è multitype come nell'esempio...

  5. #5
    Codice PHP:
    <?php
    class A
        public 
    $b=''
        public function 
    __construct($b){ 
            
    $this->b=$b
        } 
           public function 
    __call($m$a){
            print 
    "Method $m called:\n";
            
    //var_dump($a);
            
    return ucfirst($this->b);
        }
        function 
    __toString(){ 
            return (string)
    $this->b
        } 

    $s= new A('ciao'); 
    echo 
    $s//ciao 
    var_dump($s->first_letter()); //Ciao
    ?>
    dai un occhio a questo thread .

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

  6. #6
    Utente di HTML.it
    Registrato dal
    Feb 2005
    Messaggi
    209
    Ho letto la pillola, non romperò quando proverò a fare l'overloading ma non vedo come posso risolvere il problema.
    Il __toString a quanto pare ritorna solo (string), ma io vorrei che la mia istanza fosse un'array.
    Quindi l'esempio più calzante è la classe Array:

    Codice PHP:
    $a=new Array(array(1,2,3,4));
    $a->push('d');
    echo 
    $a[4]; //d 
    Anzi la mia classe vorrei che proprio funzionasse così, come quella degli Array.

    Se il primo post che ho scritto fosse stato questo, sarebbe stato meglio

  7. #7
    Utente di HTML.it
    Registrato dal
    Feb 2005
    Messaggi
    209
    Qualche idea?

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.