Domanda

mi sono reso conto che non c'è downcasting in PHP5. C'è un modello comune per raggiungerlo?

È stato utile?

Soluzione

È possibile impostare la classe derivata di prendere un oggetto BaseClass come parametro nel costruttore, e quindi copiare i oggetti di che:

class Base {
    var $x, $y;
}

class DerivedClass extends Base {
    function __construct($param) {
         $this->copyFromBase($param); // put some type-checking here...
    }

    function copyFromBase($base) {
        $this->x = $base->x;    // you could definitely use a more
        $this->y = $base->y;    // intelligent way to do this
    }
}

$b = new Base();
$b->x = 'X';
$b->y = 'Y';
$b = new Derived($b);
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top