No, it is not possible (before PHP 8.0): if you want to pass the third parameter, you have to pass the second one. And named parameters are not possible either.
A "solution" would be to use only one parameter, an array, and always pass it... But don't always define everything in it.For instance :
function foo($params) {
var_dump($params);
}
And calling it this way : (Key / value array)
foo([
'a' => 'hello',
]);
foo([
'a' => 'hello',
'c' => 'glop',
]);
foo([
'a' => 'hello',
'test' => 'another one',
]);
Will get you this output :
array
'a' => string 'hello' (length=5)
array
'a' => string 'hello' (length=5)
'c' => string 'glop' (length=4)
array
'a' => string 'hello' (length=5)
'test' => string 'another one' (length=11)
But I don't really like this solution :
So I'd go with this only in very specific cases -- for functions with lots of optional parameters, for instance...
That is an awfully long method for something that is so simple. PHP 8 has added named arguments. When you unpack an array to be used as arguments, its keys are used as parameter names. doesn't accept named arguments like you are passing it.mysqli_stmt::bind_result()
If we simplify this code then it should look something like this:
/**
* Dynamically executes a given sql statement as prepared statement (?-placeholder).
* Expects correct parameters as an array to replace ?.
* Returns an array with ($arr[index]['column_name'] = value), or null.
*/
function dynamic_db_reader(mysqli $ms, array $params, string $qry): ?array
{
// Replace prefix (DBPREF in: inc/config.php)
if (strpos($qry, 'prefix_') !== false) {
$qry = str_replace('prefix', DBPREF, $qry);
}
$stmt = $ms->prepare($qry);
// Dynamically bind parameters from $params
if ($params) {
$stmt->bind_param(str_repeat('s', count($params)), ...$params);
}
$stmt->execute();
return $stmt->get_result()->fetch_all(MYSQLI_ASSOC) ?: null;
}
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$mysqli = new mysqli('localhost', 'user', 'password', 'test');
$mysqli->set_charset('utf8mb4');
$results = dynamic_db_reader($mysqli, ['foo'], 'SELECT ?');
If for some reason you are using mysqli compiled from libmysql client, then ... well, it's time to figure out how to enable mysqlnd or switch over to PDO.
P.S. Please make sure that you have mysqli error reporting enabled. How to get the error message in MySQLi?. Also, there is no point in setting the charset each time. Set it right after making the connection.
In php8 you have named arguments.
https://github.com/dannyvankooten/PHP-Router/blob/master/src/Route.php#L184
Calls , which accepts the arguments as array.call_user_func_array
In php < 8 an associative array was used like a normal indexed array, since the keys hadnt any meaning. In php8 the keys have to match the variable names of your function.
To get your code working you simply have to get rid of the keys:
$collection->attachRoute(new PHPRouter\Route('/ajax/dropzone', [
'_controller' => 'App\Controllers\Ajax\Dropzone::Dropzone',
'methods' => ['POST','GET'],
'parameters'=> ['template_file'=>'ajax'],
^---------------^ remove this.
]));
Example.
This is literally called the operator in PHP, but is known as the splat operator from other languages. From a 2014 LornaJane blog post on the feature:...
This feature allows you to capture a variable number of arguments to a function, combined with "normal" arguments passed in if you like. It's easiest to see with an example:
function concatenate($transform, ...$strings) { $string = ''; foreach($strings as $piece) { $string .= $piece; } return($transform($string)); } echo concatenate("strtoupper", "I'd ", "like ", 4 + 2, " apples");
(This would print )I'D LIKE 6 APPLES
The parameters list in the function declaration has the
operator in it, and it basically means " ... and everything else should go into...". You can pass 2 or more arguments into this function and the second and subsequent ones will be added to the $strings$strings, ready to be used.array
It's a PHP 8 >= feature now: https://wiki.php.net/rfc/named_params
<?php
// Using positional arguments:
array_fill(0, 100, 50);
// Using named arguments:
array_fill(start_index: 0, num: 100, value: 50);
No. PHP does not support named parameters. Only the order of parameters is taken into account. You could probably take the code itself apart using the ReflectionClass to inspect the function parameter names, but in the end you'd need to use this to reorder the array anyway.