如何在属于另一个类实例的类实例中调用他外面类的函数

WBOY
Release: 2016-06-23 14:06:00
Original
1040 people have browsed it

标题可能没描述清楚  下面是伪代码

class a {    public $xx = 0;    public $b_list = [];    public function add_b ($param) {        $this->b_list [] = new b($param);    }	public function pp() {		$xx++	}}class b extend a {    public $set = 0;    public function __init($param) {        //忘了构造函数怎么写了吧这个相信成构造函数        $set = $param;    }    public function pp () {        //want to make class a $xx ++        //.....           }}$a = new a;$a->add_b(111);$a->add_b(222);//now $a has 2 $b//下面是调用第一个$b$a->b_list[0]->pp();//如何在pp中实现 $a->xx ++  ?
Copy after login


回复讨论(解决方案)

    public function pp() {
        $ this->xx++
    }

忘了构造函数 __construct 的写法,可以直接用类名
class b extend a {
    public $set = 0;
    public function  b($param) {

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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!