简体   繁体   English

为什么我的 PHP 孩子 class 没有从父母那里获得公共和受保护的变量?

[英]Why is my PHP child class not getting public and protected vars from the parent?

I'm having brainfreeze, and I suspect this one is really simple.我正在大脑冻结,我怀疑这个真的很简单。 Consider this code, with two classes:考虑这段代码,有两个类:

<?php    
class myparentclass {
    protected $vara;
    private $varb;
    public $varc;
    public $_childclass;

    function __construct() {
        $this->vara = "foo";
        $this->varb = "bar";
        $this->varc = ":(";
        $this->_childclass = new mychildclass;    
    }
}

class mychildclass extends myparentclass {
    function __construct() {
        print_r ($this);
    }
}

print "<pre>";
$foo = new myparentclass();

The output is: output 是:

mychildclass Object
(
    [vara:protected] => 
    [varb:private] => 
    [varc] => 
    [_childclass] => 
)

I know $varb shouldn't be set, but what about the others?我知道不应该设置 $varb,但是其他的呢?

If you define a new __construct() in the child class as you've done to print vars out, you need to explicitly call the parent's constructor too.如果您在子 class 中定义了一个新的__construct() ,就像您已经完成打印 vars 一样,您还需要显式调用父的构造函数。 If you did not define any __construct() in the child class, it would directly inherit the parent's and all those properties would have been set.如果您没有在子 class 中定义任何__construct() ,它将直接继承父的并且所有这些属性都将被设置。

class mychildclass extends myparentclass {
  function __construct() {
    // The parent constructor
    parent::__construct();
    print_r ($this);
  }
}

You have to call the parent class constructor inside the child class constructor.您必须在子 class 构造函数中调用父 class 构造函数。

function __construct() {
        parent::__construct();
        print_r ($this);
    }

If you re-define the constructor in your child class, you have to call the parent constructor.如果在子 class 中重新定义构造函数,则必须调用父构造函数。

class mychildclass extends myparentclass {
 function __construct() {
     parent::__construct();
     print_r ($this);
 }
}

Should work fine.应该工作正常。

If the child class has it's own constructor, you must explicitly call the parent constructor from within it (if you want it called):如果子 class 有自己的构造函数,则必须从其中显式调用父构造函数(如果要调用它):

parent::__construct();

Your parent constructor is never executed by the child.你的父构造函数永远不会被孩子执行。 Modify mychildclass like this:像这样修改 mychildclass:

function __construct() {
    parent::__construct();
    print_r ($this);
}

You're overriding the parent class' constructor with the constructor in your parent class.您正在使用父 class 中的构造函数覆盖父类的构造函数。 You can call the parent's constructor from your child class with parent::__construct();您可以使用 parent::__construct(); 从您的孩子 class 调用父母的构造函数;

However the last line of the constructor of myparentclass calls the constructor of mychildclass which in turn calls the parent constructor, et cetera.然而,myparentclass 的构造函数的最后一行调用了 mychildclass 的构造函数,而 mychildclass 的构造函数又调用了父构造函数,等等。 Did you mean to achieve this?你的意思是实现这一目标吗?

<?php    
class myparentclass {
    protected $vara;
    private $varb;
    public $varc;

    function __construct() {
        $this->vara = "foo";
        $this->varb = "bar";
        $this->varc = ":(";
    }
}

class mychildclass extends myparentclass {
    function __construct() {
        parent::__construct();
        print_r ($this);
    }
}

print "<pre>";
$foo = new mychildclass();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM