TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 04-25-2008, 10:05 PM   #1 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default 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 is offline  
Reply With Quote
Old 04-25-2008, 10:07 PM   #2 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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.
delayedinsanity is offline  
Reply With Quote
Old 04-25-2008, 11:10 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

How about this guide?
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
The Following User Says Thank You to Wildhoney For This Useful Post:
delayedinsanity (04-25-2008)
Old 04-25-2008, 11:29 PM   #4 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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.
delayedinsanity is offline  
Reply With Quote
Old 04-26-2008, 04:35 PM   #5 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

Haha! Something like that! I can be quite devilish in my spare time.
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 05-03-2008, 08:16 PM   #6 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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
delayedinsanity is offline  
Reply With Quote
Old 05-03-2008, 08:36 PM   #7 (permalink)
The Contributor
 
Gibou's Avatar
 
Join Date: Nov 2007
Location: France, near Paris
Posts: 53
Thanks: 6
Gibou is on a distinguished road
Default

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;
}
?>
__________________
Wedus project's Website
Send a message via MSN to Gibou
Gibou is offline  
Reply With Quote
The Following User Says Thank You to Gibou For This Useful Post:
delayedinsanity (05-03-2008)
Old 05-03-2008, 08:42 PM   #8 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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
delayedinsanity is offline  
Reply With Quote
Old 05-03-2008, 08:50 PM   #9 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

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); 
Salathe is offline  
Reply With Quote
Old 05-03-2008, 09:06 PM   #10 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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 is offline  
Reply With Quote
Old 05-07-2008, 05:04 AM   #11 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

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
delayedinsanity is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:49 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design