繁体   English   中英

如何从另一个类调用一个类的函数?

[英]How to call a function of a class from another class?

我想知道如何在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新手,但我发现没有什么可以帮助我的。

这很容易,只需执行以下操作:

$this-> index(<parameter>);

我强烈建议您查看PHP文档: http : //php.net/manual/zh/language.oop5.basic.php

从同一个班级,您可以像这样打电话

$this->index($appointment_hash);

您可以在backend_api中扩展约会类,也不要在backend_api控制器中将函数名称创建为index()

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

}

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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