dongpiao0731 2018-03-19 11:09
浏览 156
已采纳

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

I tried creating an object in PHP for PHPMailer to be used in development enviroments.

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.
         );
    }
}

Then I call the class in another controller:

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
            }
        }

for some reason, it just dumps an empty object. I also tried using

  var_dump($config->ReceiverEmail);
  • 写回答

3条回答 默认 最新

  • dongyin6576 2018-03-19 11:16
    关注

    Constructors do not work that way. They do not have a return value – http://php.net/manual/en/language.oop5.decon.php

    new ClassA always returns an instance of that class.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 基于52单片机的酒精浓度检测系统加继电器和sim800
  • ¥50 我撰写的python爬虫爬不了 要爬的网址有反爬机制
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答