Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Correct way to call mysqli using Intelephense

Tags:

php

mysqli

This code triggers my editor's intelephense for error:

/**
 * Connect to database
 */
public function link() {
    global $config; mysqli_report(MYSQLI_REPORT_ERROR);
    try {
        return new \mysqli($config['db_hostname'], $config['db_username'], $config['db_password'], $config['db_name']);
    } catch (\exception $e) {
        throw new \exception($e->getMessage(), $e->getCode());
    }
}

Expected 6 arguments. Found 4.intelephense(10005)

would it be fine if I just use:

return new \mysqli($config['db_hostname'], $config['db_username'], $config['db_password'], $config['db_name'],null,null);

Thank you all for answering; also deceze who corrected me on wrong way to catch exception;

this is edited code:

/**
 * Connect to database
 */
public function link() {
    global $config; mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
    try {
        return new \mysqli($config['db_hostname'], $config['db_username'], $config['db_password'], $config['db_name'], ini_get('mysqli.default_port'), ini_get('mysqli.default_socket'));
    } catch (\exception $e) {
        echo 'Cannot connect to a database server'; die();
    }
}

note ,this is for withing the class using namespaces...

like image 616
Milan Rilex Ristic Avatar asked Dec 08 '25 10:12

Milan Rilex Ristic


1 Answers

The intelephense plugin uses the stubs from PhpStorm. The author already submitted a PR to fix this (and other functions with optional parameters): https://github.com/JetBrains/phpstorm-stubs/pull/520.

As soon as that's merged and the stubs are updated, you should no longer receive the problem reported in vscode.

There should be no need to change your constructor call, it is valid code and will execute without problems.

like image 181
webmaster777 Avatar answered Dec 09 '25 23:12

webmaster777