如何从构造函数中获取变量,以便它可以在其他函数中使用


How To Get Variable From Constructor So It Can Be Used In Other Functions?

我有这段代码:

<?php
class Email{
    public $mandrill_host;
    public function __construct() {
        $this->config_ini = parse_ini_file($_SERVER['DOCUMENT_ROOT'] . '/config.ini', true);
        $this->mandrill_host = $config_ini['mandrill']['host'];
    }
    public function sendEmail () {
        $res = $this->mandrill_host;
        return $res;    
    }
}
$test = new Email;
echo $test->sendEmail ();
?>

它给了我一个空的结果。 构造函数方法似乎没有给出函数sendEmail所需的变量。 即使我已经在类级别声明为公共变量。

如何从构造函数获取$this->mandrill_host,以便我可以在任何其他方法中使用它? 我在这里错过了什么?

尝试

class Email{
    public $mandrill_host;
    public $config_ini; //you are missing this
    public function __construct() {
        $this->config_ini = parse_ini_file($_SERVER['DOCUMENT_ROOT'] . '/config.ini', true);
        $this->mandrill_host = $this->config_ini['mandrill']['host'];
    }
    public function sendEmail () {
        $res = $this->mandrill_host;
        return $res;    
    }
}
$test = new Email;
echo $test->sendEmail ();
?>