1

Good Morning @everybody, I am trying to Create a function on a namespaced class which returns a 'resource' type, however, I am failing to do it.

What I tried to do:#1

public function SQLQuery($query):resource
{
    // the magic...
}

to be used with the folllowing syntax:

$SQL = SQLQuery('SELECT * FROM my_db');

but it resulted in:

syntax error, unexpected ':', expecting ';' or '{' because i'm running PHP 5.6 and that feature requires PHP 7...

so i have tried another method but it didn't work with me too:#2

public function SQLQuery($query, &$resrc)
{
    $resrc = mysqli_query($SQLDB, $query);
    // the magic...
}

SQLQuery('SELECT * FROM my_db', $resrc_var);

so i have tried another method which worked for me at least basiclly:#3

public function SQLQuery($query)
{
    $resrc = mysqli_query($SQLDB, $query);
    // the magic...
    return $resrc;
}

$SQL = SQLQuery('SELECT * FROM my_db');
Zorono
  • 75
  • 2
  • 12
  • I don't get the error that you are saying it returns. Are you sure that error is for that snippet? – Script47 Jun 01 '18 at 23:52
  • The error I get is: `Uncaught TypeError: Return value of Test::SQLQuery() must be an instance of resource, none returned in /run_dir/repl.php(68) : eval()'d code:5 Stack trace: #0 /run_dir/repl.php(68) : eval()'d code(8): Test->SQLQuery('test') #1 /run_dir/repl.php(68): eval() #2 {main} thrown` – Script47 Jun 01 '18 at 23:52
  • @Script47 yes, iam sure (XAMPP's PHP 5.6) – Zorono Jun 02 '18 at 00:08
  • Please check my answer. – Script47 Jun 02 '18 at 00:09

1 Answers1

1

As per your comment, you are using PHP 5.6, however return types were introduced in PHP 7+. Therefore, to use that feature, you will need to update to PHP 7 or higher.

Script47
  • 14,230
  • 4
  • 45
  • 66