Home  >  Article  >  Backend Development  >  php 子类重写父类值?

php 子类重写父类值?

WBOY
WBOYOriginal
2016-06-06 20:37:33891browse

<code>    class A extends B {

    public function __construct($config) {
        foreach ($config as $k => $v) {
            echo $k;
            var_dump(isset($this->$k));
            if (isset($this->$k)) {
                $this->$k = $v;
            }
        }
        exit;


class B {

    public $appid;
    public $templateId;
    public $api;
    public $accountSid;
    public $token;
    public $version;
</code>

打印的结果
appidbool(false)
templateIdbool(false)
apibool(false)
accountSidbool(false)
tokenbool(false)
versionbool(false)

isset($this->$k) 为什么是false

回复内容:

<code>    class A extends B {

    public function __construct($config) {
        foreach ($config as $k => $v) {
            echo $k;
            var_dump(isset($this->$k));
            if (isset($this->$k)) {
                $this->$k = $v;
            }
        }
        exit;


class B {

    public $appid;
    public $templateId;
    public $api;
    public $accountSid;
    public $token;
    public $version;
</code>

打印的结果
appidbool(false)
templateIdbool(false)
apibool(false)
accountSidbool(false)
tokenbool(false)
versionbool(false)

isset($this->$k) 为什么是false

用isset()判断一个不存在的值或是值为NULL的变量,将返回false。
在基类B中,只是声明了appid等变量,没有赋具体的值。

php 子类重写父类值?

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn