簡體   English   中英

如何在PHP中調用另一個類函數

[英]How to call another class function in PHP

我的問題類似於來自另一個類PHP的函數中的Call函數 ,但其答案對我不起作用。

基本上這就是我想要做的。

在“ a.php”中,我定義了A類。

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

在“ b.php”中,我想定義B類,該類可以調用A類中的函數。這就是我所說的,但似乎不起作用。

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

誰能指出我正確的方向? 謝謝

========

更新:為了使我的問題更清楚,我舉了另一個例子。

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;  
  }
}

這樣嘗試

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM