0
我意識到php5中沒有向下轉換。是否有共同的模式來實現它?在php5中向下轉換
我意識到php5中沒有向下轉換。是否有共同的模式來實現它?在php5中向下轉換
您可以設置派生類從該採取BaseClass的對象作爲構造函數的參數,然後複製屬性:
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);