Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
php5中的向下投射_Php_Downcast - Fatal编程技术网

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... }

我已经意识到php5中没有降级。是否有一个共同的模式来实现它

您可以将派生类设置为将基类对象作为构造函数中的参数,然后从中复制属性:

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