我如何在codeigniter中访问该函数之外的变量

How can i access a variable outside that function in codeigniter

很抱歉问了这么基础的问题。我从数据库中获取了一些数据并将其存储到函数内的一个变量中,并想在函数外获取该变量的值?

public function getemailData()
{
    $email_id_investors = $this->db
        ->select('value')
        ->get_where('common_email_settings', ['name' => investors_email])
        ->row()->value;

}

我希望在函数外获取 $email_id_investors 的值。对于这个基本问题,我再次表示歉意

数据库table名称 - common_email_settings

字段是 ID、标题、名称、值

1 封问候邮件,greeting_email,问候语@investorsloanservicing.com

2 贷款服务邮件,loan_service_email,LoanServicing@investorsloanservicing.com

3 处理邮件,processing_email,处理@investorsloanservicing.com

为了严格回答这个问题,您可以将值存储在一个范围内的全局 $this 变量中,但我不知道您为什么不查询函数并拥有它 return一个值。

public function getemailData($investors_email)
{
    $this->email_id_investors = $this->db
        ->select('value')
        ->get_where('common_email_settings', ['name' => $investors_email])
        ->row()->value;
}

// then in another function called later in the chain, you can grab it
public function doSomethingElse() {
  $investors =  $this->email_id_investors;
} 

最好只为该变量创建一个 getter 函数。

根据您的情况,这看起来没有用。如果您存储的变量是处理器密集型变量(并且您将 $this 用作缓存),这可能很有用,您需要在给定状态期间调用的多个函数中访问它,并且您不想重写你的函数链接受这个参数(以便传递它)。但是,这是一个明显的迹象,您需要重构逻辑并使其更加灵活(例如,传递对象或数组而不是单个变量)。

您没有返回变量。 尝试像这样返回变量,

public function getemailData()
{
    $email_id_investors = $this->db
        ->select('value')
        ->get_where('common_email_settings', ['name' => investors_email])
        ->row()->value;
     return $email_id_investors;


}
function getemailData($name)
{
    
    $email_id_investors = $this->db
        ->get_where('common_email_settings', ['name' => $name])
        ->result()[0];
    return $email_id_investors->value;
}

这个对我有用。这个功能我已经在通用模型页面给出了,其他人也调用了这个pages.Thank求你的帮助

    $email = $this->common->getemailData('account_email'); -> getting data in this variable
    echo $email;
    // exit();