Home > Archive > PHP Programming > December 2004 > Passing Variable Length Argument List to Parent
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
Passing Variable Length Argument List to Parent
|
|
|
|
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 ....
...
..
| |
| Michael Fesser 2004-12-08, 3:56 pm |
| .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
| |
| Joshua Beall 2004-12-08, 3:56 pm |
| "Michael Fesser" <netizen@gmx.net> wrote in message
news:hf5er09fhdskuf07ap47s3frcchs7cg8ad@
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!
| |
|
| Joshua Beall wrote:
> "Michael Fesser" <netizen@gmx.net> wrote in message
> news:hf5er09fhdskuf07ap47s3frcchs7cg8ad@
4ax.com...
>
> 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.
| |
|
| 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.
| |
| Michael Fesser 2004-12-18, 12:49 pm |
| .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
| |
|
| 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.
|
|
|
|
|