Wrapper method for mysqli_bind_param - php

I'm writing a Database wrapper class and need something like:
public function bind($types, $params, ...) {
$this->prep->bind_param($types, $params, ...);
}
How can I make the arguments dynamic, to have N-params?
I know of the function func_get_args() but doesn't help, I can fetch the arguments, but how to pass?

Off the top of my head, you could do it using call_user_func_array():
public function bind() {
$args=func_get_args();
$method=array($this->prep,'bind_param');
call_user_func_array($method,$args);
}

The function call_user_func_array should be what you need, something along the lines of the following:
public function bind () {
$args = func_get_args();
call_user_func_array(array($this->prep, "bind_param"), $args);
}
call_user_func and call_user_func_array can sometimes be a little slower than calling a method directly, unfortunately there isn't much you can do about this apart from hard code in the first few arguments.

Use an array, i advise you to use the classic way and keep some "core" arguments away from the array, than you can put optionnal ones in an array like so:
function function(Class $object, array $options = array()){
}

Related

How to receive specific interface's array in PHP class method?

I want to receive array of MyInterface as the in the below code.
public function saveMultiple(
\Path\To\MyInterface $attachLinks[]
);
The above code doesn't work.
So please don't tell me that just remove \Path\To\MyInterface and to use a variable $attachLinks. I'm already aware of that but this is something which I require.
There are no generic types in php, so you can not specify type «array of something».
But you may do a trick. Check types of elements in array.
array_walk($attachLinks, function (\Path\To\MyInterface $item) {});
If you wrap it in assert, you will be able to disable this check.
assert('array_walk($attachLinks, function (\Path\To\MyInterface $item) {})');
So far this is what can be done using PHP7. Till date passing arrays with specific type is not possible.
public function saveMultiple(
array $attachLinks
);
It'll now at least make sure that the method only gets array as parameter.
Maybe you can use some type of setter like:
private $attachLinks = [];
public function setter(MyInterface $var)
{
$this->attachLinks[] = $var;
}
And than use a $this->attachLinks in your function
public function saveMultiple() {
print_r($this->attachLinks);
}

Pass by reference, with a variable numbers of parameters in PHP?

I have a function that takes a variable number of parameters, and I have to pass them by reference to another function.
Such as
function my_function($arg0, $arg1, $arg2, ...)
{
my_other_function(&$arg0, &$arg1, &$arg2, ...);
}
So that when I pass things by reference to my_function, my_other_function also gets them by reference.
Is there a way to do this?
I wonder why you need this. In general references are bad in PHP.
If you really want to do this the only proper way (ignoring call-time pass-by-ref hacks, which won't work with PHP 5.4 anymore anyways) is to use an array wrapping the parameters:
function myfunc(array $data) {
$data[0] += 42;
/* ... */
}
$var = 0;
myfunc(array(&$var /*, ... */));
echo $var; // prints 42
For passing to the other function you can then use call_user_func_array()
Technically you are not doing this:
function my_function($arg0, $arg1, $arg2, ...)
{
my_other_function(&$arg0, &$arg1, &$arg2, ...);
}
but this:
function my_function(&$arg0, &$arg1, &$arg2, ...)
{
my_other_function($arg0, $arg1, $arg2, ...);
}
However as #johannes already wrote, there is no good support for variable number of arguments that are references in PHP (and who could know better). func_get_argsDocs for example does not work with references.
Instead make use of the suggestion to pass all references via an array parameter. That works.
function my_function(array $args)
{
my_other_function($args);
}

php, I cant pass an array as reference

this is the function:
public function func(&$parameters = array())
{
}
now I need to do this:
$x->func (get_defined_vars());
but that fails. Another way:
$x->func (&get_defined_vars());
it drops an error: Can't use function return value in write context in ...
Then how to do it?
get_defined_vars() returns an array, not a variable. As you can only pass variables by reference you need to write:
$definedVars = get_defined_vars();
func($definedVars);
Though I don't really see a reason to pass the array by reference here. (If you are doing this for performance, don't do it, as it won't help.)
public function func(&$parameters = array())
{
}
Not defined correctly.
Try this way:-
call_user_func_array( 'func', $parameters );
See the notes on the call_user_func_array() function documentation for more information.

Get number of parameters a function requires

This is an extension question of PHP pass in $this to function outside class
And I believe this is what I'm looking for but it's in python not php: Programmatically determining amount of parameters a function requires - Python
Let's say I have a function like this:
function client_func($cls, $arg){ }
and when I'm ready to call this function I might do something like this in pseudo code:
if function's first parameter equals '$cls', then call client_func(instanceof class, $arg)
else call client_func($arg)
So basically, is there a way to lookahead to a function and see what parameter values are required before calling the function?
I guess this would be like debug_backtrace(), but the other way around.
func_get_args() can only be called from within a function which doesn't help me here.
Any thoughts?
Use Reflection, especially ReflectionFunction in your case.
$fct = new ReflectionFunction('client_func');
echo $fct->getNumberOfRequiredParameters();
As far as I can see you will find getParameters() useful too
Only way is with reflection by going to http://us3.php.net/manual/en/book.reflection.php
class foo {
function bar ($arg1, $arg2) {
// ...
}
}
$method = new ReflectionMethod('foo', 'bar');
$num = $method->getNumberOfParameters();

Passing a function's args straight to another function

In PHP, is there a way to send a function's arguments straight to another function without having to specify them one by one? Is there a way to expand func_get_arg() so that the other function receives the individual arguments and not just a single array?
I'd like to send the arguments from foo() straight to bar() like so:
function foo($arg1, $arg2, $arg3)
{
$args = expand_args(func_get_arg());
bar($args);
}
yes.
function foo($arg1, $arg2, $arg3)
{
$args = func_get_arg();
call_user_func_array("bar",$args);
}
If you want to call it on a function belonging to an instance of an entirely seperate class, that can be done by passing the first arg to call_user_func_array as an array.
In this example, the function foo accepts whatever arguments, and passes them directly into $bar->baz->bob(), and returns the result.
public function foo(/* example arguments */)
{
return call_user_func_array
(
array($bar->baz, 'bob'),
func_get_args()
);
}

Categories