php OOP 父子关系

发布于 2024-09-19 00:31:25 字数 358 浏览 9 评论 0原文

class base{
 public $c = 'c';
 public $sub  = '';
 function __construct(){
    $this->sub = new sub();
 }
}

class sub extends base{
 public $ab = 'abs';
 function __construct(){
  $this->c = 'aas';
  echo 'Test';
 }
}


$a = new base();
print_r($a);

我想让子类编辑基本变量 $this->c = 'blabla';

我怎样才能实现这一点?

class base{
 public $c = 'c';
 public $sub  = '';
 function __construct(){
    $this->sub = new sub();
 }
}

class sub extends base{
 public $ab = 'abs';
 function __construct(){
  $this->c = 'aas';
  echo 'Test';
 }
}


$a = new base();
print_r($a);

I would like to the sub class to edit the base vars $this->c = 'blabla';

how can i achieve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半枫 2024-09-26 00:31:25

为什么不直接覆盖它:

class sub extends base
{
    public $ab = 'abs';
    public $c = 'blabla';
}

否则,如果您需要修改实际的基本属性,请按照 Wrikken 的建议使用 parent

Why not just override it:

class sub extends base
{
    public $ab = 'abs';
    public $c = 'blabla';
}

Otherwise, if you need to modify the actual base property, use parent as Wrikken suggested.

倾听心声的旋律 2024-09-26 00:31:25

这不会是我引以为傲的代码(不同的构造函数签名),但这会起作用(单次使用):

class base{
 public $c = 'c';
 public $sub  = '';
 function __construct(){
    $this->sub = new sub($this);
 }
}

class sub extends base{
 public $ab = 'abs';
 function __construct($parent){
  $parent->c = 'aas';
  echo 'Test';
 }
}

如果您更频繁地需要它:

class base{
 private $parent;
 private $top;
 public $c = 'c';
 public $sub  = '';
 function __construct(base $parent = null, base $top = null){
    $this->parent = $parent;
    $this->top    = $top;
    $this->addSub();
 }
 function addSub(){
    $this->sub    = new sub($this,$this->top ? $this->top : $this);
 }

}

class sub extends base{
 public $ab = 'abs';
 function __construct($parent,$top){
  parent::__construct($parent,$top);
  $this->parent->c = 'aas';
 }
 function foo($bar){
    $this->top->c = $bar;
 }
 //preventing infinite recursion....
 function addSub(){
 }
}

根据实际需求,另一种设计模式可能更适合。

't wouldn't be code I was proud of (different constructor signatures), but this would work (single use):

class base{
 public $c = 'c';
 public $sub  = '';
 function __construct(){
    $this->sub = new sub($this);
 }
}

class sub extends base{
 public $ab = 'abs';
 function __construct($parent){
  $parent->c = 'aas';
  echo 'Test';
 }
}

If you need it more often:

class base{
 private $parent;
 private $top;
 public $c = 'c';
 public $sub  = '';
 function __construct(base $parent = null, base $top = null){
    $this->parent = $parent;
    $this->top    = $top;
    $this->addSub();
 }
 function addSub(){
    $this->sub    = new sub($this,$this->top ? $this->top : $this);
 }

}

class sub extends base{
 public $ab = 'abs';
 function __construct($parent,$top){
  parent::__construct($parent,$top);
  $this->parent->c = 'aas';
 }
 function foo($bar){
    $this->top->c = $bar;
 }
 //preventing infinite recursion....
 function addSub(){
 }
}

Depending on what the actual needs are, another design pattern is likely more suited.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文