By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,852 Members | 2,123 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,852 IT Pros & Developers. It's quick & easy.

Passing Variable Length Argument List to Parent

P: n/a

hello!

I have a class method that takes a variable number of parameters and
correctly deals with them with func_get_args, etc ...

i.e.

class ABC
{
public function MooSaysTheCow()
{
foreach (func_get_args() as $arg_name => $arg_value)
{
...
}
}
}

now, the problem is: i want to override this class and this method, have
that overriding method call the base class and then do something else. The
problem is that I don't know how to pass the parameters along.

class DEF
{
public function MooSaysTheCow()
{
parent::MooSaysTheCow( WHAT DO I PUT HERE ????? );
echo "Overridden version with new exciting features!";
}
}
Any ideas?

the best i've thought of so far is terribly yucky, and not flexible to
arbitrary numbers of parameters .... blech.

class DEF
{
public function MooSaysTheCow()
{
switch (func_get_num_args())
{
case 1:
parent::MooSaysTheCow(func_get_arg(0));
break;
case 2:
parent::MooSaysTheCow(func_get_arg(0), func_get_arg(1));
break;
etc ....
...
..
Jul 17 '05 #1
Share this Question
Share on Google+
11 Replies


P: n/a
Mark wrote:
hello!

I have a class method that takes a variable number of parameters and
correctly deals with them with func_get_args, etc ...

i.e.

class ABC
{
public function MooSaysTheCow()
{
foreach (func_get_args() as $arg_name => $arg_value)
{
...
}
}
}

now, the problem is: i want to override this class and this method, have
that overriding method call the base class and then do something else. The
problem is that I don't know how to pass the parameters along.
Check the comments for
<http://www.php.net/manual/en/function.func-get-args.php>

class DEF
{
public function MooSaysTheCow()
{
parent::MooSaysTheCow( WHAT DO I PUT HERE ????? );
echo "Overridden version with new exciting features!";
}
}
Any ideas?

the best i've thought of so far is terribly yucky, and not flexible to
arbitrary numbers of parameters .... blech.

class DEF
{
public function MooSaysTheCow()
{
switch (func_get_num_args())
{
case 1:
parent::MooSaysTheCow(func_get_arg(0));
break;
case 2:
parent::MooSaysTheCow(func_get_arg(0), func_get_arg(1));
break;
etc ....
...
..
.
}
}
}
thanks.
mark.

Jul 17 '05 #2

P: n/a
did you try to pass the return value of func_get_args()?

"Mark" <mw@ANGRYLanfear.com> wrote in message
news:Is********************@nventure.com...

hello!

I have a class method that takes a variable number of parameters and
correctly deals with them with func_get_args, etc ...

i.e.

class ABC
{
public function MooSaysTheCow()
{
foreach (func_get_args() as $arg_name => $arg_value)
{
...
}
}
}

now, the problem is: i want to override this class and this method, have
that overriding method call the base class and then do something else.
The
problem is that I don't know how to pass the parameters along.

class DEF
{
public function MooSaysTheCow()
{
parent::MooSaysTheCow( WHAT DO I PUT HERE ????? );
echo "Overridden version with new exciting features!";
}
}
Any ideas?

the best i've thought of so far is terribly yucky, and not flexible to
arbitrary numbers of parameters .... blech.

class DEF
{
public function MooSaysTheCow()
{
switch (func_get_num_args())
{
case 1:
parent::MooSaysTheCow(func_get_arg(0));
break;
case 2:
parent::MooSaysTheCow(func_get_arg(0), func_get_arg(1));
break;
etc ....
...
..
.
}
}
}
thanks.
mark.
--
I am not an ANGRY man. Remove the rage from my email to reply.

Ashlee Simpson Sings! You Faint .... --More--
Ashlee Simpson Sings! Ashlee Simpsons sings! You die ... --More--

Jul 17 '05 #3

P: n/a
On Tue, 07 Dec 2004 15:37:52 -0800, Mark <mw@ANGRYLanfear.com> wrote:
I have a class method that takes a variable number of parameters and
correctly deals with them with func_get_args, etc ...

i.e.

class ABC
{
public function MooSaysTheCow()
{
foreach (func_get_args() as $arg_name => $arg_value)
{
...
}
}
}

now, the problem is: i want to override this class and this method, have
that overriding method call the base class and then do something else. The
problem is that I don't know how to pass the parameters along.

class DEF
{
public function MooSaysTheCow()
{
parent::MooSaysTheCow( WHAT DO I PUT HERE ????? );
echo "Overridden version with new exciting features!";
}
}
Any ideas?

the best i've thought of so far is terribly yucky, and not flexible to
arbitrary numbers of parameters .... blech.

class DEF
{
public function MooSaysTheCow()
{
switch (func_get_num_args())
{
case 1:
parent::MooSaysTheCow(func_get_arg(0));
break;
case 2:
parent::MooSaysTheCow(func_get_arg(0), func_get_arg(1));
break;
etc ....
...
..
.
}
}
}


What am I missing here; can't you just use func_get_args() to pass the
arguments upwards?

--
Andy Hassall / <an**@andyh.co.uk> / <http://www.andyh.co.uk>
<http://www.andyhsoftware.co.uk/space> Space: disk usage analysis tool
Jul 17 '05 #4

P: n/a
Dani CS wrote:

Check the comments for
<http://www.php.net/manual/en/function.func-get-args.php>


Close!

func_get_args() doesn't work because the parent function has to then be
expecting an array parameter, not a list of parameters as normal (and i do
NOT have control over the parent implementation -- it's a prepackaged
class).

i was moderately hopeful when I saw the function call_user_func_array,
which does what i want -- takes an array with parameters and calls the
function with the array values as normal arguments

EXCEPT -- that won't work within classes, expeshully in the __construct
method ...

argh.

any other ideas?
thanks!
mark.

Jul 17 '05 #5

P: n/a
I'm typing this without testing it, but it seems like it would work. I'm
sure you take a performance hit from the eval() call, though.

$argList = '';
for($n=0;$n<func_get_num_args();$n++)
$argList .= "func_get_arg($n),";
$argList = rtrim($argList,',');
/* $argList will not be a string that looks like
"func_get_art(0),func_get_arg(1),func_get_arg( 2)", repeating for however
many arguments have been passed*/
eval("parent::MooSaysTheCow($argList);");

Worth a shot.
Jul 17 '05 #6

P: n/a
.oO(Mark)
I have a class method that takes a variable number of parameters and
correctly deals with them with func_get_args, etc ... [...]

now, the problem is: i want to override this class and this method, have
that overriding method call the base class and then do something else. The
problem is that I don't know how to pass the parameters along.

class DEF
{
public function MooSaysTheCow()
{
parent::MooSaysTheCow( WHAT DO I PUT HERE ????? );
echo "Overridden version with new exciting features!";
}
}


Try

public function MooSaysTheCow() {
$args = func_get_args();
call_user_func_array(array('parent', 'MooSaysTheCow'), $args);
}

Micha
Jul 17 '05 #7

P: n/a
"Michael Fesser" <ne*****@gmx.net> wrote in message
news:hf********************************@4ax.com...
Try

public function MooSaysTheCow() {
$args = func_get_args();
call_user_func_array(array('parent', 'MooSaysTheCow'), $args);
}


Very clever. Mark, please report back and let us know if this works for
you!
Jul 17 '05 #8

P: n/a
Joshua Beall wrote:
"Michael Fesser" <ne*****@gmx.net> wrote in message
news:hf********************************@4ax.com...
Try

public function MooSaysTheCow() {
$args = func_get_args();
call_user_func_array(array('parent', 'MooSaysTheCow'), $args);
}


Very clever. Mark, please report back and let us know if this works for
you!


nope. probably would have in PHP4, but most certainly not in PHP5.

i'm still stuck with the switch statement in PHP5, unfortunately.

argh!
mark.
--
I am not an ANGRY man. Remove the rage from my email to reply.
Jul 17 '05 #9

P: n/a
Andy Hassall wrote:
On Tue, 07 Dec 2004 15:37:52 -0800, Mark <mw@ANGRYLanfear.com> wrote:

What am I missing here; can't you just use func_get_args() to pass the
arguments upwards?


no. at least not in overloaded methods in classes.

the problem is that the overloadER and the overloadEE are both looking for
variable argument lists. func_get_args() returns an ARRAY.

now, the obvious (lame) solution would be to modify both functions to just
take arrays of arguments but ... i don't have control over the base class's
implementation (it's the mysqli class).

so, i appear to be stuck with the switch statement way of doing things.
d'oh!

thanks,
mark.

--
I am not an ANGRY man. Remove the rage from my email to reply.
Jul 17 '05 #10

P: n/a
.oO(Mark)
nope. probably would have in PHP4, but most certainly not in PHP5.
Hmm. The following works here on PHP5 when called statically:

class TBar {
function doSomething() {
print_r(func_get_args());
}
}

class TFoo extends TBar {
function doSomething() {
$args = func_get_args();
call_user_func_array(array('parent', 'doSomething'), $args);
}
}

TFoo::doSomething(23, 42);

If called on an instance of TFoo my PHP crashes. But if I declare the
above methods 'static' then it works, even if invoked non-statically ...
i'm still stuck with the switch statement in PHP5, unfortunately.


You could also try it with eval(), as already mentioned in another post.

Micha
Jul 17 '05 #11

P: n/a
Michael Fesser wrote:

Hmm. The following works here on PHP5 when called statically:

class TBar {
function doSomething() {
print_r(func_get_args());
}
}

class TFoo extends TBar {
function doSomething() {
$args = func_get_args();
call_user_func_array(array('parent', 'doSomething'), $args);
}
}

TFoo::doSomething(23, 42);
aaaah. the problem is that i am trying to do this in the constructor. of
the mysqli class.

argh.

such is life :-).

You could also try it with eval(), as already mentioned in another post.
!aaaieee!

might be the only solution.

thanks much!
Micha


--
I am not an ANGRY man. Remove the rage from my email to reply.
Jul 17 '05 #12

This discussion thread is closed

Replies have been disabled for this discussion.