TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Passing multiple arguments to a function via one variable. (http://www.talkphp.com/advanced-php-programming/2709-passing-multiple-arguments-function-via-one-variable.html)

delayedinsanity 04-25-2008 10:05 PM

Passing multiple arguments to a function via one variable.
 
Without setting up null possibilities for every argument, is it possible to pass multiple arguments to a function call via one variables?

ie, in my singleton loader, I have the following;

PHP Code:

$aInstance[$szClass] = new $szClass($szArgs); 

Okay, so now I can pass one argument to the object. What if I wanted to pass two? At first I thought I had the brilliant idea of making $szArgs -> $aArgs instead. Well passing the array straight in there obviously isn't going to work. It's just going to get the word "array" as the argument.

So I thought, what if you reconstructed the array into a string, using the arguments quoting convention of "arg", "arg2", "etc". Should've known before I started trying that it wouldn't work. It just passes it as a string, like it should.

So... I wonder if there's any way to pass multiple arguments to a function call or object call, with only one variable?

This is more for curiosity's sake than anything. I don't have any classes at this point that I even pass a single argument too when I call them. Just one of those random thoughts, "can I do that??".
-m

delayedinsanity 04-25-2008 10:07 PM

To try and simplify that further, I guess what I'm trying to do is have $szArgs interpreted before it gets passed to the method.

Wildhoney 04-25-2008 11:10 PM

How about this guide?

delayedinsanity 04-25-2008 11:29 PM

Oh, oh, I see how it is.

That article never existed before. I posted this, and you were all, hey, why don't I write an article about it, and then you made it look like you posted that way back last year, and that other people found it highly useful as well. ALL just to make me look silly, didn't ya?

Didn't YA?!
Thank you.

Wildhoney 04-26-2008 04:35 PM

Haha! :-) Something like that! I can be quite devilish in my spare time.

delayedinsanity 05-03-2008 08:16 PM

Can I do this through two function calls? Ie, if my first call is a static

core::load('module', 'arg1', 'arg2');

Can I then, from within load somehow pass arg1 and arg2?

$pPointer = new object($args)

? I know how to grab the muliple arguments from load using func_get_args/func_num_args, but then I want to pass it on again, using a single variable if possible. Or do I have to do something like

PHP Code:

switch (func_num_args()) {
    case 
1$pPointer = new object(func_get_arg(0));
    case 
2$pPointer = new object(func_get_arg(0), func_get_arg(1));
    default: ....


Basically I'm not passing the arguments straight to the function or object I want to call, they're going through an intermediary which needs to pass them on again. I know I just repeated myself a few times there, but I'm trying to work it out in my head, and I'm at the school right now so I don't have my handy test server around to throw attempts at. I stopped using theirs ever since they randomly deleted my site off of it by accident, and since it took them a month to get back to me on some requests I had.
-m

Gibou 05-03-2008 08:36 PM

Excuse me if I haven't well understood but why don't you use something like that:

PHP Code:

<?php
$pPointer
=new Object(fung_get_args());
?>

And in the Object constructor, you parse the array with a generic constructor like that:

PHP Code:

<?php
if(is_array($args))
{
  foreach(
$args as $k=>$v)
    if(
property_exists($this,$k))
      
$this->$k $v;
}
?>


delayedinsanity 05-03-2008 08:42 PM

That's not a bad idea at all. I guess I was looking for a way to pass multiple arguments via my load() method without having to do any extra coding in the object itself to handle it. That way my objects could be clean, and the load method would handle all the organization. So I supposed with that idea in mind I could do something such as

PHP Code:

$aArgs array_splice(func_get_args(), 3);
$pPointer = new object($aArgs); 

and just drop your code into the __construct of my object.
-m

Salathe 05-03-2008 08:50 PM

Rather than re-wiring all of your classes to accept a single argument in the form of an array, you could use PHP5's Reflection API to create a new object instance with our arguments like:
PHP Code:

// Equivalent of $object = new Object(...);
$reflect = new ReflectionClass('Object');
$object  $reflect->newInstanceArgs($args); 


delayedinsanity 05-03-2008 09:06 PM

Okay, *that* may be a little beyond my scope at this point. I like to fully understand the things I'm doing before I do them, otherwise when things break I can sit there banging my head for ever before I find one simple little mistake I made haha. I'm checking out the manual for that right now, and I just got my book today, "PHP Objects, Patterns and Practice" which has a bit on the Reflection API, so I'll look into that, and see what I learn. Thanks!
-m

delayedinsanity 05-07-2008 05:04 AM

Okay, so I'm into the section of the Reflection API in my book now. You could've told me your code works just as is! haha. ;-)

PHP Code:

if (!array_key_exists($szClassself::$Library)) {

    if (
is_array($mArgs)) {
        
$pReflect = new ReflectionClass($szClass);
        
self::$Library[$szClass] = $pReflect->newInstanceArgs($mArgs);
    } else {
        
self::$Library[$szClass] = new $szClass($mArgs);
    }



and now I can do

PHP Code:

$pMultiple kePhoto::load("test""multiple", array("one""two""three"));

or

$pSingle kePhoto::load("test""single""one"); 

and it works great! Thank you Salathe.
-m


All times are GMT. The time now is 05:58 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0