2009-11-12 66 views

回答

1

您可以設置派生類從該採取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);