I am working on a PHP class that provides functions for my postgres database.
My class Database
looks like this:
<?php
declare(strict_types=1);
namespace App\Services\Database;
include_once('../server/Config.php');
include_once('../server/App/Services/Database/DatabaseConfiguration.php');
use \App\Services\Database\DatabaseConfiguration;
class Database
{
private DatabaseConfiguration $config;
private \PgSql\Connection|false $connection;
public function __construct(?string $host, ?int $port, ?string $dbName, ?string $user, ?string $password)
{
$this->config = new DatabaseConfiguration($host, $port, $dbName, $user, $password);
}
public function connect(): bool
{
$this->connection = pg_connect($this->config->connectionInfo());
if ($this->connection) {
return true;
} else {
return false;
}
}
}
So nothing fancy, but my IDE (Visual Studio Code & PHP Intelephense) throws an error when it comes to the connection with the database.
Undefined type 'PgSql\Connection'.
This line forces the error:
private \PgSql\Connection|false $connection;
According to the documentation (here), the function pg_connect
returns the type PgSql\Connection|false
. Because my class is wrapped in a namespace I added a \
in front. But both notations throw the error.
Before I was using PG I used MySql and with that I had no issue when it comes to typing.
So how can I run my IDE with PG typings?
Setup: Win10, PHP 8.1.2, PHP Intelephense v1.8.2
Edit: In the extension settings there is this tab:
Intelephense: Stubs Configure stub files for built in symbols and common extensions. The default setting includes PHP core and all bundled extensions.
-> pgsql is added there.