可捕获的致命错误:参数 1 通过



我写了这段代码:

索引.php:

(我的索引(

<?php

namespace com;

$loader = require __DIR__ . '/../vendor/autoload.php';
$loader->add(__NAMESPACE__, __DIR__ . '/../../');
$loader->register();
$config = require __DIR__ . "/../config/mainConfig.php";
new frameworkcom($config);

框架\com:

(我的框架主类。 此类获取配置(

<?php

namespace comframework;

use comframeworkDTOIConfig;
class com {
    /**
     * @var IConfig
     */
    private $config;
    /**
     * @param IConfig $config
     */
    public function __construct(IConfig $config)
    {
        $this->config = $config;
    }
}

com\framework\DTO\IConfig:

(日期转移对象(

<?php

namespace comframeworkDTO;

class IConfig {
    /** @var  string  */
    public $projectName;
    /** @var  string  */
    public $projectDescription;
    /** @var  string  */
    public $projectAuthor;
    /** @var array  */
    public $sqlConnectionVariables = array("user", "pass", "options", "connectionString");
}

config/mainConfig.php:

(我的主要配置(

<?php

namespace comconfig;

class mainConfig {
    /** @var string  */
    public $projectName = "example";
    /** @var string  */
    public $projectDescription = "example";
    /** @var string  */
    public $projectAuthor = "example";
    /** @var array  */
    public $sqlConnectionVariables = [
        "user" => "root",
        "pass" => "",
        "options" => [
            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        ],
        "connectionString" => "mysql:host=localhost;dbname=example;charset=utf8"
    ];
}

错误:

Catchable fatal error: Argument 1 passed to comframeworkcom::__construct() must be an instance of comframeworkDTOIConfig, integer given, called in C:wampwwwcomwebindex.php on line 12 and defined in C:wampwwwcomframeworkcom.php on line 18

我该如何解决这个问题?我需要将"mainConfig.php"发送到"com"类,但它不起作用。

发生这种情况是因为如果require成功,它的返回值将为1

一种可能的修复方法可能如下所示:

require __DIR__ . "/../config/mainConfig.php";
$config = new configmainConfig();
new frameworkcom($config);

还要使配置扩展类型提示接口(如 sjagr 在评论中所说(:

class mainConfig extends comframeworkDTOIConfig

然后看看还有什么失败。

你需要这样做:

namespace comconfig;
class mainConfig extends comframeworkDTOIConfig {
    /** @var string  */
    public $projectName = "example";
    /** @var string  */
    public $projectDescription = "example";
    /** @var string  */
    public $projectAuthor = "example";
    /** @var array  */
    public $sqlConnectionVariables = [
        "user" => "root",
        "pass" => "",
        "options" => [
            PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES 'UTF8'",
            PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
        ],
        "connectionString" => "mysql:host=localhost;dbname=example;charset=utf8"
    ];
}
return new mainConfig();

但我建议您下次使用真正的接口(和单例(。

相关内容

  • 没有找到相关文章

最新更新