php5中的向下投射
我已经意识到php5中没有降级。是否有一个共同的模式来实现它 您可以将派生类设置为将基类对象作为构造函数中的参数,然后从中复制属性:php5中的向下投射,php,downcast,Php,Downcast,我已经意识到php5中没有降级。是否有一个共同的模式来实现它 您可以将派生类设置为将基类对象作为构造函数中的参数,然后从中复制属性: class Base { var $x, $y; } class DerivedClass extends Base { function __construct($param) { $this->copyFromBase($param); // put some type-checking here... }
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);
您可以将派生类设置为将基类对象作为构造函数中的参数,然后从中复制属性:
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);