pass by reference variable length args
pass by reference variable length args
am 06.01.2010 04:48:42 von kalinga
hi all,
i'm trying to write a wrapper function for "mysqli_stmt_bind_results".
and expect it to work the same way it accepts and bind results to the
original function.
the problem is, i couldn't find a way to pass the args by reference
via func_get_args and register the out put from call_user_func_array
to the caller scope.. any idea?
here goes few lines which i'm trying hard for past 48 hours.. with no luck.. :(
class stmt {
private $stmt;
public function bind_result() {
$argsToBindResult = func_get_args();
$argList = array($this->stmt);
$argList = array_merge($argList, $argsToBindResult);
call_user_func_array('mysqli_stmt_bind_result', $argList);
}
}
$stmt->prepare('SELECT name,email FROM users WHERE id = ?');
$stmt->bind_param('i',2);
... ..
$stmt->bind_result($name,$email);
echo $name,$email;
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: pass by reference variable length args
am 06.01.2010 05:01:53 von Robert Cummings
viraj wrote:
> hi all,
> i'm trying to write a wrapper function for "mysqli_stmt_bind_results".
> and expect it to work the same way it accepts and bind results to the
> original function.
>
> the problem is, i couldn't find a way to pass the args by reference
> via func_get_args and register the out put from call_user_func_array
> to the caller scope.. any idea?
>
> here goes few lines which i'm trying hard for past 48 hours.. with no luck.. :(
>
> class stmt {
> private $stmt;
>
> public function bind_result() {
> $argsToBindResult = func_get_args();
> $argList = array($this->stmt);
> $argList = array_merge($argList, $argsToBindResult);
> call_user_func_array('mysqli_stmt_bind_result', $argList);
> }
> }
>
> $stmt->prepare('SELECT name,email FROM users WHERE id = ?');
> $stmt->bind_param('i',2);
> .. ..
> $stmt->bind_result($name,$email);
> echo $name,$email;
You're out of luck for using the func_get_args() call. The following
method (albeit a dirty method) works:
function bind_stuff( &$arg1, &$arg2=null, &$arg3=null, &$arg4=null,
&$arg5=null, &$arg6=null, &$arg7=null, &$arg8=null, &$arg9=null )
{
for( $i = 1; $i <= 9; $i++ )
{
${'arg'.$i} = 'bound'.$i;
}
}
bind_stuff( $name, $email );
echo $name."\n";
echo $email."\n";
?>
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: pass by reference variable length args
am 06.01.2010 06:04:48 von kalinga
thanks rob!
here goes the working method.
public function bind_result(&$arg1=3Dnull,&$arg2=3Dnull,&$arg3=3Dnull) {
$numArgs =3D func_num_args();
$args =3D array();
for ($i=3D1; $i<=3D $numArgs; $i++) {
$args['arg' . $i] =3D &${'arg'.$i};
}
call_user_func_array('mysqli_stmt_bind_result',array_merge(a rray($this->s=
tmt),$args));
}
$stmt->bind_result($id, $test);
$stmt->fetch();
echo $id, $test;
~viraj
On Wed, Jan 6, 2010 at 10:01 AM, Robert Cummings wro=
te:
> viraj wrote:
>>
>> hi all,
>> i'm trying to write a wrapper function for "mysqli_stmt_bind_results".
>> and expect it to work the same way it accepts and bind results to the
>> original function.
>>
>> the problem is, i couldn't find a way to pass the args by reference
>> via func_get_args and register the out put from call_user_func_array
>> to the caller scope.. any idea?
>>
>> here goes few lines which i'm trying hard for past 48 hours.. with no
>> luck.. :(
>>
>> class stmt {
>> Â Â Â private $stmt;
>>
>> Â Â Â public function bind_result() {
>> Â Â Â Â Â Â Â Â $argsToBindResult=
=3D func_get_args();
>> Â Â Â Â Â Â Â Â $argList =3D arra=
y($this->stmt);
>> Â Â Â Â Â Â Â Â $argList =3D arra=
y_merge($argList, $argsToBindResult);
>> Â Â Â Â Â Â Â Â call_user_func_ar=
ray('mysqli_stmt_bind_result', $argList);
>> Â Â Â Â }
>> }
>>
>> $stmt->prepare('SELECT name,email FROM users WHERE id =3D ?');
>> $stmt->bind_param('i',2);
>> .. ..
>> $stmt->bind_result($name,$email);
>> echo $name,$email;
>
> You're out of luck for using the func_get_args() call. The following meth=
od
> (albeit a dirty method) works:
>
>
>
> function bind_stuff( &$arg1, &$arg2=3Dnull, &$arg3=3Dnull, &$arg4=3Dnull,
> &$arg5=3Dnull, &$arg6=3Dnull, &$arg7=3Dnull, &$arg8=3Dnull, &$arg9=3Dnull=
)
> {
> Â Â for( $i =3D 1; $i <=3D 9; $i++ )
> Â Â {
> Â Â Â Â ${'arg'.$i} =3D 'bound'.$i;
> Â Â }
> }
>
> bind_stuff( $name, $email );
>
> echo $name."\n";
> echo $email."\n";
>
> ?>
>
> Cheers,
> Rob.
> --
> http://www.interjinn.com
> Application and Templating Framework for PHP
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: pass by reference variable length args
am 06.01.2010 06:18:11 von kalinga
if func_get_args supports pass by reference, we could have avoid the
loop and pre-defined arg list.
something like..
extract(func_get_args) :D
cheers!
~viraj
On Wed, Jan 6, 2010 at 11:04 AM, viraj wrote:
> thanks rob!
>
> here goes the working method.
>
> public function bind_result(&$arg1=3Dnull,&$arg2=3Dnull,&$arg3=3Dnull) {
> Â Â Â Â Â Â Â Â $numArgs =3D func_=
num_args();
> Â Â Â Â Â Â Â Â $args =3D array();
> Â Â Â Â Â Â Â Â for ($i=3D1; $i<=
=3D $numArgs; $i++) {
> Â Â Â Â Â Â Â Â Â Â =C2=
=A0 Â $args['arg' . $i] =3D &${'arg'.$i};
> Â Â Â Â Â Â Â Â }
>
> Â Â Â Â Â Â Â Â call_user_func_arr=
ay('mysqli_stmt_bind_result',array_merge(array($this->stmt), $args));
> }
>
> Â Â Â Â $stmt->bind_result($id, $test);
> Â Â Â Â $stmt->fetch();
> Â Â Â Â echo $id, $test;
>
> ~viraj
>
>
> On Wed, Jan 6, 2010 at 10:01 AM, Robert Cummings w=
rote:
>> viraj wrote:
>>>
>>> hi all,
>>> i'm trying to write a wrapper function for "mysqli_stmt_bind_results".
>>> and expect it to work the same way it accepts and bind results to the
>>> original function.
>>>
>>> the problem is, i couldn't find a way to pass the args by reference
>>> via func_get_args and register the out put from call_user_func_array
>>> to the caller scope.. any idea?
>>>
>>> here goes few lines which i'm trying hard for past 48 hours.. with no
>>> luck.. :(
>>>
>>> class stmt {
>>> Â Â Â private $stmt;
>>>
>>> Â Â Â public function bind_result() {
>>> Â Â Â Â Â Â Â Â $argsToBindResul=
t =3D func_get_args();
>>> Â Â Â Â Â Â Â Â $argList =3D arr=
ay($this->stmt);
>>> Â Â Â Â Â Â Â Â $argList =3D arr=
ay_merge($argList, $argsToBindResult);
>>> Â Â Â Â Â Â Â Â call_user_func_a=
rray('mysqli_stmt_bind_result', $argList);
>>> Â Â Â Â }
>>> }
>>>
>>> $stmt->prepare('SELECT name,email FROM users WHERE id =3D ?');
>>> $stmt->bind_param('i',2);
>>> .. ..
>>> $stmt->bind_result($name,$email);
>>> echo $name,$email;
>>
>> You're out of luck for using the func_get_args() call. The following met=
hod
>> (albeit a dirty method) works:
>>
>>
>>
>> function bind_stuff( &$arg1, &$arg2=3Dnull, &$arg3=3Dnull, &$arg4=3Dnull=
,
>> &$arg5=3Dnull, &$arg6=3Dnull, &$arg7=3Dnull, &$arg8=3Dnull, &$arg9=3Dnul=
l )
>> {
>> Â Â for( $i =3D 1; $i <=3D 9; $i++ )
>> Â Â {
>> Â Â Â Â ${'arg'.$i} =3D 'bound'.$i;
>> Â Â }
>> }
>>
>> bind_stuff( $name, $email );
>>
>> echo $name."\n";
>> echo $email."\n";
>>
>> ?>
>>
>> Cheers,
>> Rob.
>> --
>> http://www.interjinn.com
>> Application and Templating Framework for PHP
>>
>
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: pass by reference variable length args
am 06.01.2010 06:30:41 von Robert Cummings
viraj wrote:
> if func_get_args supports pass by reference, we could have avoid the
> loop and pre-defined arg list.
>
> something like..
>
> extract(func_get_args) :D
Absolute! I'm not sure why there isn't some kind of way to retrieve a
reference in this manner, but I suspect it's related to knowing which
parameters were set as reference appropriate values. For instance
contrast the following:
bind_stuff( $id, $test );
Versus the following:
bind_stuff( 5, $test );
Defining the function parameters to use references will generate a fatal
error when 5 is passed since this is a fatal error. How then to enable
variable args to achieve the same result? I guess one could ask PHP to
support something like the following:
$args = func_bind_args( array( true, true ) );
Where $args would then contain a reference where the corresponding index
was set to true in the argument list. Since this isn't very variable,
then the last index set would denote the default for all other args.
Thus the example above could be shortened:
$args = func_bind_args( array( true ) );
Or even:
$args = func_bind_args( true );
Since this is run-time too, then PHP could generate an E_WARNING instead
of E_FATAL and bind a copy instead when no reference was passed.
On further thought, the current func_get_args() could be adapted in this
manner since it currently accepts no arguments.
Anyways... just thoughts. I hit this problem in the past too :)
Cheers,
Rob.
--
http://www.interjinn.com
Application and Templating Framework for PHP
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php