note 71506 added to functions.arguments

From: Date: Sat, 02 Dec 2006 21:43:41 +0000
Subject: note 71506 added to functions.arguments
Groups: php.notes 
Request: Send a blank email to php-notes+get-120240@lists.php.net to get a copy of this message
In some cases (like object method overloading) I've wanted to pass a variable number of arguments to a function. PHP solves this problem by insisting on passing a single array of arguments, which can be annoying if you already have functions written that expect a certain number of arguments. Example: <? class Someclass { __call($m,$a){ $this->targetFunction($m,implode($a,',')); // doesn't work, with or without quotes } function targetFunction($target,$var,$anotherVar){ .... } } Someclass::target('var','anotherVar'); ?> The only solution I've found is to expect an array of arguments in any function that may be called like this, substituting it with func_get_args() if neccesary like so: <? class Someclass { __call($m,$a){ $this->targetFunction($m,$a); } function targetFunction ( $target, $a ) { if ( !is_array( $a ) ) $a = func_get_args(); ... } } Someclass::target('var','anotherVar'); ?> I hope someone has a better method for this, it gets messy :-( Kieran ---- Server IP: 66.163.161.117 Probable Submitter: 74.12.100.71 ---- Manual Page -- http://www.php.net/manual/en/functions.arguments.php Edit -- https://master.php.net/note/edit/71506 Del: integrated -- https://master.php.net/note/delete/71506/integrated Del: useless -- https://master.php.net/note/delete/71506/useless Del: bad code -- https://master.php.net/note/delete/71506/bad+code Del: spam -- https://master.php.net/note/delete/71506/spam Del: non-english -- https://master.php.net/note/delete/71506/non-english Del: in docs -- https://master.php.net/note/delete/71506/in+docs Del: other reasons-- https://master.php.net/note/delete/71506 Reject -- https://master.php.net/note/reject/71506 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#120240) next »