PHP5 downcasting

1 Cevap php

Ben hiçbir downcasting php5 içinde var olduğunu fark ettik. Bunu başarmak için ortak bir model var mı?

1 Cevap

Siz yapıcı bir parametre olarak bir AnaSınıf nesne almak için türetilmiş bir sınıf set, ve o gelen özelliklerini kopyalamak olabilir:

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);