2015-11-24 127 views
1

我想知道我怎麼能叫在Appointments類可我index功能,結構是這樣的:如何從另一個類調用一個類的函數?

class Appointments extends CI_Controller 
{ 
public function __construct() 
{ 
    parent::__construct(); 
    $this->load->library('session'); 

    if ($this->session->userdata('language')) 
    { 
     $this->config->set_item('language', $this->session->userdata('language')); 
     $this->lang->load('translations', $this->session->userdata('language')); 
    } 
    else 
    { 
     $this->lang->load('translations', $this->config->item('language')); 
    } 
} 

/** 
* @param string $appointment_hash 
*/ 

public function index($appointment_hash = '') 
{... 

從名爲Backend_api另一個類。通常用於調用一個函數,我加載模型,但在這種情況下,我沒有模型,但只有一個類。對不起,如果問題是愚蠢的,但我在CodeIgniter新,我沒有發現任何可以幫助我。

回答

0

從同一類,你可以這樣調用

$this->index($appointment_hash); 

你可以在backend_api中擴展約會類,也不要在backend_api控制器中創建一個函數名稱作爲index()

class Backend_api extends Appointments { 
public function __construct() 
{ 
    parent::__construct(); 
    $this->index($appointment_hash); 

} 

} 
+0

現在的問題是我已經有了一個擴展,現在我怎麼可以擴展多個類? – Dillinger

+0

你不能(http://php.net/manual/en/keyword.extends.php) 爲了解決這個問題,你可以在上面建議的Backend_api中創建一個Appointments類的實例。 更好的方法是使用Dependancy Injection:http://php-di.org/doc/understanding-di.html/code.tutsplus.com/tutorials/dependency-injection-in-php--net -28146 –

0

這是非常容易的,只是做:

$this-> index(<parameter>); 

我強烈建議你看看PHP文件:http://php.net/manual/en/language.oop5.basic.php

+0

沒有人,我不在同一個班級中我想調用位於Appointments類中的索引函數Backend_api – Dillinger

+0

道歉,從你的代碼示例看,它看起來很像你是在相同的上課,但我沒有正確地評論你的評論。 如果您試圖訪問另一個函數,您可以隨時在Backend_api中創建一個Appointments類的實例...或者讓index()函數實現一個統計函數。 –

相關問題