Home > Backend Development > PHP Tutorial > 怎么在子类用父类的魔术方法

怎么在子类用父类的魔术方法

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-23 13:52:55
Original
972 people have browsed it

父类:P

class P{    private $name="";    function __construct(){        $this->name="hello";    }    public function __set($name, $value){                $this->$name=$value;          }    public function showName(){        echo $this->name;    }}
Copy after login

子类:C
class C extends P{    function __construct(){        parent::__construct();        //想在这里给P类的$name换个值(你好)怎么做?    }    }
Copy after login


$c=new C;
$c->showName;
然后输出:你好
怎么实现?


回复讨论(解决方案)

class P{    private $name="";    function __construct(){        $this->name="hello";    }    public function __set($name, $value){                $this->$name=$value;          }    public function showName(){        echo $this->name;    }}class C extends P{    function __construct(){        parent::__construct();        $this->name = '你好';    }    }$c=new C;$c->showName();print_r($c);
Copy after login
你好
C Object
(
[name:P:private] => 你好
)

class P{    private $name="";    function __construct(){        $this->name="hello";    }    public function __set($name, $value){        $this->$name=$value;    }    public function showName(){        echo $this->name;    }}class C extends P{    function __construct(){        parent::__construct();         //想在这里给P类的$name换个值(你好)怎么做?		 $this->name = '你好';    }}$obj = new C();$obj->showName();
Copy after login

Related labels:
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