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

php 子类重写父类值?

WBOY
Release: 2016-06-06 20:37:33
Original
921 people have browsed it

<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>
Copy after login
Copy after login

打印的结果
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>
Copy after login
Copy after login

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

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

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

php 子类重写父类值?

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template