-1

i write this code:

index.php:

(My Index)

<?php


namespace com;


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

$config = require __DIR__ . "/../config/mainConfig.php";
new framework\com($config);

framework\com:

(My Framework Main Class. This class get the config)

<?php


namespace com\framework;


use com\framework\DTO\IConfig;

class com {
    /**
     * @var IConfig
     */
    private $config;

    /**
     * @param IConfig $config
     */
    public function __construct(IConfig $config)
    {

        $this->config = $config;
    }
}

com\framework\DTO\IConfig:

(Date Transfer Obect)

<?php


namespace com\framework\DTO;


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:

(My Main Config)

<?php


namespace com\config;


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"
    ];
}

The error:

Catchable fatal error: Argument 1 passed to com\framework\com::__construct() must be an instance of com\framework\DTO\IConfig, integer given, called in C:\wamp\www\com\web\index.php on line 12 and defined in C:\wamp\www\com\framework\com.php on line 18

How can i fix this? i need send "mainConfig.php" to "com" class, and it's not working.

EqWey Ace
  • 5
  • 4
  • `class mainConfig extends com\framework\DTO\IConfig`... but you must modify how you do the properties in `mainConfig` – sjagr Dec 23 '14 at 14:08
  • There's no `return` statement in `mainConfig.php`. So `require` just returns `1`. What are you expecting to be in `$config`? – Barmar Dec 23 '14 at 14:12

2 Answers2

0

This happens because a require will have a return value of 1 if it succeeds.

One possible fix could look like:

require __DIR__ . "/../config/mainConfig.php";
$config = new config\mainConfig();
new framework\com($config);

Also make the config extend the type hinted interface (as sjagr said in the comment):

class mainConfig extends com\framework\DTO\IConfig

And then see what else fails.

Alin Purcaru
  • 43,655
  • 12
  • 77
  • 90
0

You need to do that:

namespace com\config;

class mainConfig extends \com\framework\DTO\IConfig {
    /** @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();

But I would suggest you to use real interfaces next time (and singletons).

Gnucki
  • 5,043
  • 2
  • 29
  • 44