如何从构造函数返回值并将值传递给另一个用户定义的函数

时间:2014-04-10 08:08:05

标签: php

我有这个构造函数__construct()

class MxpCms_Admin {

    public function __construct()
    {       
        global $MxpDatabase;

        $Qcmsdrag = $MxpDatabase->query('select t.id from :table_templates t, :table_configuration c where t.code = c.configuration_value');
        $Qcmsdrag->bindTable(':table_templates', TABLE_TEMPLATES);
        $Qcmsdrag->bindTable(':table_configuration', TABLE_CONFIGURATION);
        $Qcmsdrag->bindValue(':boxes_group', 'left');
        $Qcmsdrag->execute();

        $load_template_id = $Qcmsdrag->value('id');
        //error_log(print_r($Qcmsdrag,TRUE));
        return $load_template_id;
    }}

返回$ load_template_id; //包含我想在下面的代码中使用的值

下面我正在检查会话是否已设置,否则执行其他部分。

if(!isset($_SESSION['cms_id']))
    {
        $loadTemplateId = MxpCms_Admin::__construct();
        $MxpBoxesSelected = $MxpCms->getSelectedLayout($loadTemplateId);
        $MxpIdSelected = $MxpCms->getSelectedLayoutId($loadTemplateId);
    }
    else
    {
        $MxpBoxesSelected = $MxpCms->getSelectedLayout($_SESSION['cms_id']);
        $MxpIdSelected = $MxpCms->getSelectedLayoutId($_SESSION['cms_id']);
    }

但是我得到了致命的错误。

致命错误:非静态方法MxpCms_Admin :: __ construct()无法静态调用 我对如何返回值感到困惑。

2 个答案:

答案 0 :(得分:0)

class MxpCms_Admin {

    protected $load_template_id;

    public function __construct()
    {       
        global $MxpDatabase;

        $Qcmsdrag = $MxpDatabase->query('select t.id from :table_templates t, :table_configuration c where t.code = c.configuration_value');
        $Qcmsdrag->bindTable(':table_templates', TABLE_TEMPLATES);
        $Qcmsdrag->bindTable(':table_configuration', TABLE_CONFIGURATION);
        $Qcmsdrag->bindValue(':boxes_group', 'left');
        $Qcmsdrag->execute();

        $this->load_template_id = $Qcmsdrag->value('id');
        //error_log(print_r($Qcmsdrag,TRUE));
    }

    public function getId(){
        return $this->load_template_id;
    }
}

然后

$Admin = new MxpCms_Admin();
$loadTemplateId = $Admin->getId();

$loadTemplateId = (new MxpCms_Admin())->getId();

取决于PHP的版本

或(甚至更好)

class MxpCms_Admin {

    protected $MxpDatabase;

    public function __construct($MxpDatabase)
    {       
        $this->MxpDatabase = $MxpDatabase;
   }

    public function getId(){
        $Qcmsdrag = $this->MxpDatabase->query('select t.id from :table_templates t, :table_configuration c where t.code = c.configuration_value');
        $Qcmsdrag->bindTable(':table_templates', TABLE_TEMPLATES);
        $Qcmsdrag->bindTable(':table_configuration', TABLE_CONFIGURATION);
        $Qcmsdrag->bindValue(':boxes_group', 'left');
        $Qcmsdrag->execute();

       return $Qcmsdrag->value('id');
    }
}

然后

$Admin = new MxpCms_Admin($MxpDatabase);
$loadTemplateId = $Admin->getId();

$loadTemplateId = (new MxpCms_Admin($MxpDatabase))->getId();

答案 1 :(得分:0)

class MxpCms_Admin {

    protected $mxpDatabase;

    public function __construct($mxpDatabase) {
        $this->mxpDatabase = $mxpDatabase;
    }

    public function getTemplateId() {
        $Qcmsdrag = $this->mxpDatabase->query('select t.id from :table_templates t, :table_configuration c where t.code = c.configuration_value');
        $Qcmsdrag->bindTable(':table_templates', TABLE_TEMPLATES);
        $Qcmsdrag->bindTable(':table_configuration', TABLE_CONFIGURATION);
        $Qcmsdrag->bindValue(':boxes_group', 'left');
        $Qcmsdrag->execute();

        return $Qcmsdrag->value('id');
    }

}

$admin = new MxpCms_Admin($mxpDatabase);
$templateId = $admin->getTemplateId();

这是对类和构造函数的更恰当的使用。我认为在TABLE_中使用两个MxpCms_Admin::getTemplateId常量仍然很糟糕,因为它依赖于全局环境而不是依赖注入。