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

accessing function arguments

P: n/a
i have the following code:

function some_function($argument)
{
validate_arg();
//more code
}

function validate_arg()
{
//this function validates the argument of some_function()
}

is it possible for validate_args() to automatically read the argument of
some_function() or do i have to note the argument in the brackets in any
case?

thx, micha

--
Jul 17 '05 #1
Share this Question
Share on Google+
16 Replies


P: n/a
micha,
is it possible for validate_args() to automatically read the argument of
some_function() or do i have to note the argument in the brackets in any
case?


No however what you can do is something like the following:
function some_function() {
if (!validate_args(func_get_args())) {
return false;
}
//some_function is valid...
}

This will send all of the arguments from some_function over to
validate_args.
Mike
Jul 17 '05 #2

P: n/a
On Thu, 19 May 2005 07:56:25 -0500, Mike Willbanks wrote:
if (!validate_args(func_get_args())) {


From the manual: "This function cannot be used directly as a function
parameter."
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #3

P: n/a
Ewoud Dronkert wrote:
On Thu, 19 May 2005 07:56:25 -0500, Mike Willbanks wrote:
if (!validate_args(func_get_args())) {

From the manual: "This function cannot be used directly as a function
parameter."

Whoops :)

then:
$args = func_get_args();
if (!validate_args($args)) {
Jul 17 '05 #4

P: n/a
Weird, that is not mentioned in my copy of the manual and you can use
func_get_args() as an argument with no problem in PHP 4. Must be
something new.

Jul 17 '05 #5

P: n/a
Yes. You can use debug_backtrace() to get the arguments to the calling
function.

function validate_arg()
$trace = debug_backtrace();
$args = $trace[1]['args'];
}

Jul 17 '05 #6

P: n/a
On 19 May 2005 09:13:51 -0700, Chung Leong wrote:
Weird, that is not mentioned in my copy of the manual and you can use
func_get_args() as an argument with no problem in PHP 4. Must be
something new.


Maybe, I never tried. It's mentioned here: http://php.net/func-get-args
--
Firefox Web Browser - Rediscover the web - http://getffox.com/
Thunderbird E-mail and Newsgroups - http://gettbird.com/
Jul 17 '05 #7

P: n/a
A user note explains it, kind of:

"func_get_arg() does not appear to be allowed to be used as a function
argument itself within class constructors in PHP 5.0.2 (wonk-ay!!!):"
Why on earth are they introducing little changes like this?

Jul 17 '05 #8

P: n/a
> A user note explains it, kind of:

"func_get_arg() does not appear to be allowed to be used as a function
argument itself within class constructors in PHP 5.0.2 (wonk-ay!!!):"
Why on earth are they introducing little changes like this?


I was looking at the manual and it says that it will work as long as it
is the first parameter in the function.

Mike
Jul 17 '05 #9

P: n/a
Chung Leong wrote:
Yes. You can use debug_backtrace() to get the arguments to the calling
function.

function validate_arg()
$trace = debug_backtrace();
$args = $trace[1]['args'];
}


But be aware that debug_backtrace() is only available from PHP 4.3.0 and
greater. If you are coding for an old version, or for a wide audience
that may be using an old version, then it may not be safe to use this
function.

--
Chris Hope | www.electrictoolbox.com | www.linuxcdmall.com
Jul 17 '05 #10

P: n/a
Seems to be the case too in PHP 4. I guess it makes some sense, as PHP
has already started pushing the arguments onto the stack if
func_get_args() comes after another argument. Interesting how PHP
pushes arguments in from left to right, whereas in C, the they get
pushed from right to left.

Jul 17 '05 #11

P: n/a
micha <ch*********@web.de> wrote in
news:Xn************************@204.153.244.170:
i have the following code:

function some_function($argument)
{
validate_arg();
//more code
}

function validate_arg()
{
//this function validates the argument of some_function()
}

is it possible for validate_args() to automatically read the argument
of some_function() or do i have to note the argument in the brackets
in any case?

thx, micha


debug_backtrace() is a very good way, because it supplies a lot of other
information too to use in my validator.

thx, micha

--
Jul 17 '05 #12

P: n/a
micha wrote:
micha <ch*********@web.de> wrote in
news:Xn************************@204.153.244.170:

i have the following code:

function some_function($argument)
{
validate_arg();
//more code
}

function validate_arg()
{
//this function validates the argument of some_function()
}

is it possible for validate_args() to automatically read the argument
of some_function() or do i have to note the argument in the brackets
in any case?

thx, micha

debug_backtrace() is a very good way, because it supplies a lot of other
information too to use in my validator.

thx, micha

Micha,
You have to specify the argument in the brackets. (Well, I
suppose it might be possible to avoid doing so, but why go
looking for trouble?)

So, your code should look like:
function some_function($argument)
{
validate_arg($argument);
//more code
}

function validate_arg($argument)
{
//this function validates the argument of some_function()
}

HTH
Jerry
Jul 17 '05 #13

P: n/a
yes, of course i can specify the argument in the brackets. call it
lazyness not to do so.

debug_backtrace() works well.

micha

Jul 17 '05 #14

P: n/a
chotiwallah wrote:
yes, of course i can specify the argument in the brackets. call it
lazyness not to do so.

debug_backtrace() works well.
However, the name of this function implies that is supposed to be used
for *debugging* purposes. While obviously you can use it for other
means, if you are using debug operations to achieve your script's main
functionality, it probably means you are going about things the wrong
way.
From a progammatic stance, if you want to pass a bunch of unknown

parameters around, either use func_get_args(), or put them into an
array.

The OP wants to validate a set (presumably of an unknown size) of
parameters with one function, which implies they are all of the same
type. In this instance, I would use an array.

--
Oli

Jul 17 '05 #15

P: n/a

Oli Filth wrote:
chotiwallah wrote:
yes, of course i can specify the argument in the brackets. call it
lazyness not to do so.

debug_backtrace() works well.
However, the name of this function implies that is supposed to be

used for *debugging* purposes. While obviously you can use it for other
means, if you are using debug operations to achieve your script's main functionality, it probably means you are going about things the wrong
way.


debug_backtrace() is a php function, same as func_get_args(). why not
use it if it is there and does the job?
if we'd only use things for what they are presumably intended, we'd
still be sitting on a tree eating only banana.
From a progammatic stance, if you want to pass a bunch of unknown

parameters around, either use func_get_args(), or put them into an
array.

The OP wants to validate a set (presumably of an unknown size) of
parameters with one function, which implies they are all of the same
type. In this instance, I would use an array.

--
Oli


i still think debug_backtrace is a good way. i'll explain how i use it.

function some_function()
{
$args = process_args();
//more code
}

function_process_args()
{
$trace = debug_backtrace();
//now $trace[1] contains calling function and it's arguments
require_one func_specs.inc.php
//validate args
//die if not valid
return $valid_args;
}

file func_spec.inc.php contains all argument info in one place, like
min and max argument count, types, allowed values, whatever.

sounds neat to me.

Jul 17 '05 #16

P: n/a
chotiwallah wrote:
<snip>
i still think debug_backtrace is a good way. i'll explain how i use it.

function some_function()
{
$args = process_args();
//more code
}

function_process_args()
{
$trace = debug_backtrace();
//now $trace[1] contains calling function and it's arguments
require_one func_specs.inc.php
//validate args
//die if not valid
return $valid_args;
}

file func_spec.inc.php contains all argument info in one place, like
min and max argument count, types, allowed values, whatever.

sounds neat to me.


Hmm?? At least for me it looks weird.

--
<?php echo 'Just another PHP saint'; ?>
Email: rrjanbiah-at-Y!com Blog: http://rajeshanbiah.blogspot.com/

Jul 17 '05 #17

This discussion thread is closed

Replies have been disabled for this discussion.