简体   繁体   中英

How to call another class function in PHP

My question is similar to Call function in function from another class PHP , but its answer does not work for me.

Basically here is what I want to do.

in 'a.php', I defined class A.

class A{
  function ab(){}
  function cd(){}
  function ef(){}
  ...
}

in 'b.php', I would like to define class B that can call the function in class A. Here is what I put, but it seems not work.

class B{
  function xy(){
    require 'a.php';
    $a = new A();
    $this->x = $a->ab();
    $this->y = $a->cd();
    return $a->ef();
  }
}

Can anyone point me to the right direction? Thanks

========

UPDATE: to make my question more clear I made another example.

class Person{
  function firstName(){return $this->firstName;}
  function lastName() {return $this->lastName;}
  function address()  {return $this->address;}
  ...
}

class Car{
  function ownerInfo(){
     $owner = array();
     require 'person.php';
     $p = new Person($this->ownerID);
     $owner['firstname'] = $p->firstName();
     $owner['lastname']  = $p->lastName();
     $owner['address']   = $p->address();
     //I am sure the data is there, but it returns nothing here
     return $owner;  
  }
}

Try like this

class B extends A{
  function xy(){
    require 'a.php';
    $a = new A();
    $this->x = $a->ab();
    $this->y = $a->cd();
    return $a->ef();
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM