PDO connection data from an external file?

There is a file with the connection class PDO and file with the config class, which contains the connection information (host, password, etc.)
Config:
<?PHP
class config {
 public $host = "localhost";
 public $base = "1111";
 public $user = "root";
 public $pass = "1111";
 public $charset = "utf8";
}
?>

Connection:
<?PHP
class db{
 public function __construct()
{
$this--->connect();
}

 private function connect()
{
 $host = $config->host;
 $dbname = $config->base;
 $charset = $config->charset;
 $user = $config->user;
 $pass = $config->pass;
 $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
 $opt = [
 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_LAZY,
 PDO::ATTR_EMULATE_PREPARES => false,
];
 $pdo = new PDO($dsn, $user, $pass, $opt);
 return $this;
}

 public function execute($sql)
{
 $sth = $pdo->prepare($sql);
 return $sth->execute();
}

 public function query($sql)
{
 $exe = $this->execute($sql);
 $res = $exe->fetch(PDO::FETCH_LAZY);

 return $res;
}
}
?>

If you specify data directly in the config file with the connection, everything works. I don't want to do. But again, when trying to connect data that is taken from a file, displays an error:
Fatal error: Uncaught PDOException: SQLSTATE[HY000] [2019] Unknown character set in E:\OSPanel\domains\test.ru\classes\_new.db.php:21 Stack trace: #0 
E:\OSPanel\domains\test.ru\classes\_new.db.php(21): PDO->__construct('mysql:host=;dbn...', NULL, NULL, Array) #1
 E:\OSPanel\domains\test.ru\classes\_new.db.php(5): db->connect() #2 
E:\OSPanel\domains\test.ru\index.php(40): db->__construct('localhost', 'root', '1111', '1111', 'utf8') #3 {main} thrown in E:\OSPanel\domains\test.ru\classes\_new.db.php on line 21

There is still avtopogruzchik indices of the classes in the file:
$config = new config;
$db = new db($config->host, $config->user, $config->pass, $config->base, $config->charset);

What to do?
June 3rd 19 at 20:22
2 answers
June 3rd 19 at 20:24
Solution
You in designer pass data to connect, but never use them. Instead, you use the variable $config which is not declared in the method context. Create a properties in the db class and in the constructor assign these properties the values obtained, and other methods or use them like so:
class db {
 private $config;

 public function __construct(Config $config)
{
 $this->config = $config;
$this->connect();
}

 private function connect()
{
 $host = $this->config->host;
...
}
}
Written according to your example, now the code looks like this:
<?PHP
class db{
 private $config;
 public function __construct(Config $config)
{
 $this--->config = $config;
$this->connect();
}

 private function connect()
{
 $host = $this->config->host;
 $dbname = $this->$config->base;
 $charset = $this->$config->charset;
 $user = $this->$config->user;
 $pass = $this->$config->pass;
 $dsn = "mysql:host=$host;dbname=$dbname;charset=$charset";
 $opt = [
 PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
 PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_LAZY,
 PDO::ATTR_EMULATE_PREPARES => false,
];
 $pdo = new PDO($dsn, $user, $pass, $opt);
 return $this;
}

 public function execute($sql)
{
 $sth = $pdo->prepare($sql);
 return $sth->execute();
}

 public function query($sql)
{
 $exe = $this->execute($sql);
 $res = $exe->fetch(PDO::FETCH_LAZY);

 return $res;
}
}
?>

And displays the error:
Fatal error: Uncaught TypeError: Argument 1 passed to db::__construct() must be an instance of config, string given, called in 
E:\OSPanel\domains\test.ru\index.php on line 40 and defined in 
E:\OSPanel\domains\test.ru\classes\_new.db.php:4 Stack trace: #0 
E:\OSPanel\domains\test.ru\index.php(40): db->__construct('localhost', 'root', '1111', '1111', 'utf8') #1 {main} thrown in E:\OSPanel\domains\test.ru\classes\_new.db.php on line 4
- Lurline.Pacocha65 commented on June 3rd 19 at 20:27
1. In the constructor you need to pass an instance of class Config
2. Not $this->$config, $this->config, as you refer to object property
3. Read the documentation for the syntax, OOP etc.
4. Google the error, they are elemental - Ocean commented on June 3rd 19 at 20:30
June 3rd 19 at 20:26
Solution
To pass to the function parameter, you have - surprise! - tell her about it.

That is, if you're going to pass to the function a value, which will then be used inside, it must be described by the function definition:
function foo($bar) {
 echo $bar;
}

But if this is not done, the function will not know how she suddenly needs to take the variable $bar.

Accordingly, in order your code earned, it is necessary to tell the designer that there will be a variable $config
public function __construct(Config $config)
And call this function in accordance with this definition. This means that when the function definition is specified 1 parameter, the call is necessary to write also the parameter 1, not 10.
That is, instead of
$db = new db($config->host, $config->user, $config->pass, $config->base, $config->charset);

must be
$db = new db($config);

Find more questions by tags PHP PDO