普通网友 2015-03-27 12:44
浏览 26

在PHP中创建子对象并将parent作为参数传递给构造

When u create an child object and you want to pass a parent object as parameter on construct you can do like in the code below:

class parent {
  protected $x;
  protected $y;
  public function __construct($x, $y) {
    $this->x = $x;
    $this->y = $y;
  }
}

class child extends parent {
 protected $z;
 public function __construct($parent, $z) {
   parent::__construct($parent->x, $parent->y);
   $this->z = $z;
 }
}

but in this case i am just multiplying parent.

Is there any way to not copy parents data and append proper object? and code to look something like this:

class child extends parent {
 protected $z;
 public function __construct($parent, $z) {
   parent = $parent;
   $this->z = $z;
 }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)