473,396 Members | 2,106 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 ....
...
..
Jul 17 '05 #1
11 4115
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
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
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
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
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
.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
"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
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
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
.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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Eric A. Forgy | last post by:
Hello, I am just learning Java and am trying to write a method that does something like //=========================================== public Static List find(double array,double val,String...
1
by: cirillo_curiosone | last post by:
Hi, i'm new to javascript. I started studing it on the web few weeks ago, but still haven't been able to solve one big problem: HOT TO PASS VALUES FROM A SCRIPT VARIABLE TO A CHILD HTML...
5
by: Greg Swindle | last post by:
Hello, I have a question about how prototyping relates to variables and their scope. Given the following code: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ var ParentObject = function() {...
3
by: Lyn | last post by:
Hi, I have a Search input form which collects from the user a person's name. I am using LIKE with a "%" suffix in the SQL so that the user does not have to type in the full name. When they hit...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
1
by: | last post by:
Hi, 1st, I did a search and could not find any info on this, the Google results were good, but I'm still have issues...So any help will be great. I have a frame page, which contains 3 frames...
6
by: ged | last post by:
Hi, i am a oo (c#) programmer, and have not used javascript for a while and i cant work out how javascript manages its references. Object References work for simple stuff, but once i have an...
6
by: virgincita schmidtmann | last post by:
Good evening, I would like to pass the size of an array from the commandline. int main(int argc, int *argv) { .... max=*argv; int list; ....
0
by: zman77 | last post by:
EDIT: -- forgot to mention... I am using Visual Studio 2005, on Win XP, on an intel machine Hi. This is my first post, though I've "lurked" for a while because I find these forums very helpful....
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.