How to call a function of an outside class in a class instance that belongs to another class instance

Source: Internet
Author: User
The title may not be described clearly below is the pseudo-code

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) {        //Forget the constructor how to write this is believed to be a constructor        $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//The following is the invocation of the first $b$a->b_list[0]->pp ();//How to implement $a in pp- >xx + +  ?


Reply to discussion (solution)

Public Function pp () {
$ this->xx++
}

Forget the __construct of the constructor function, you can use the class name directly
Class B extend a {
Public $set = 0;
Public Function B ($param) {

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.