为什么我的对象在构造和返回之后是空的?

时间:2018-03-19 11:09:31

标签: php

我尝试在PHP中为PHPMailer创建一个用于开发环境的对象。

class Configuration
    function __construct()
    {
    // creating an object for configuration, setting the configuration options and then returning it.
         return $config = (object) array(
             'DevEnv' => true,                      // DevEnv setting is used to define if PHPMailer should use a dev mail address to send to or not.
             'ReceiverEmail' => 'email@gmail.com',  // Set the develop enviroment email. 
             'ReceiverName' => 'name'              // Set the develop enviroment email name.
         );
    }
}

然后我在另一个控制器中调用该类:

protected $configuration;
function __construct()
{
    $this->configuration = new Configuration();
}
function SendInfoMail()
{
            foreach($this->configuration as $config) {
                var_dump($config);
                if ($config->DevEnv == true) {
                    // do stuff
                }else{
                    // do stuff
            }
        }

由于某种原因,它只是转储一个空对象。我也尝试过使用

  var_dump($config->ReceiverEmail);

3 个答案:

答案 0 :(得分:2)

您有Configuration类的实例。而不是那样,尝试添加新方法让我们说" getProperties()"。

class Configuration
    function getProperties()
    {
    // creating an object for configuration, setting the configuration options and then returning it.
         return $config = (object) array(
             'DevEnv' => true,                      // DevEnv setting is used to define if PHPMailer should use a dev mail address to send to or not.
             'ReceiverEmail' => 'email@gmail.com',  // Set the develop enviroment email. 
             'ReceiverName' => 'name'              // Set the develop enviroment email name.
         );
    }
}

所以你可以随时随地打电话:

protected $configuration;
function __construct()
{
    $this->configuration = new Configuration();
}
function SendInfoMail()
{
            foreach($this->configuration->getProperties() as $config) {
                var_dump($config);
                if ($config->DevEnv == true) {
                    // do stuff
                }else{
                    // do stuff
            }
        }

答案 1 :(得分:1)

构造函数不会那样工作。它们没有返回值 - http://php.net/manual/en/language.oop5.decon.php

new ClassA始终返回该类的实例。

答案 2 :(得分:1)

您正在错误地使用构造函数。请参阅此工作示例:

class Configuration {
    protected $configuration;
    function __construct() {
    // creating an object for configuration, setting the configuration options and then returning it.
         $this->configuration = (object) array(
             'DevEnv' => true,                      // DevEnv setting is used to define if PHPMailer should use a dev mail address to send to or not.
             'ReceiverEmail' => 'email@gmail.com',  // Set the develop enviroment email. 
             'ReceiverName' => 'name'              // Set the develop enviroment email name.
         );
    }
}

class Class2 {
    //protected $configuration;
    function __construct() {
        $this->configuration = new Configuration();
    }

    function SendInfoMail() {

        var_dump($this->configuration);
        foreach($this->configuration as $config) {
            if ($config->DevEnv == true) {
                // do stuff
            }else{
                // do stuff
            }
        }
    }

}


$t = new Class2();

$t->SendInfoMail();