note 70876 added to language.oop5.decon

From: Date: Thu, 02 Nov 2006 05:23:31 +0000
Subject: note 70876 added to language.oop5.decon
Groups: php.notes 
Request: Send a blank email to php-notes+get-119309@lists.php.net to get a copy of this message
Hello, I've been messing with php for about a week but am learning a lot. It occurred to me that it might be necessary in some cases to write a class that takes a variable number of arguments. After some experimentation, this example was formed: class variableArgs{ public $a = array(); protected $numOfArgs; public function __construct() { $numOfArgs=func_num_args(); if(func_num_args()==0) { $numOfArgs=1; $a[0]='No arguments passed'; $this->Arg[0]=$a[0]; } else for($i=0; $i<func_num_args(); $i++) { $a[$i]=func_get_arg($i); $this->Arg[$i]=$a[$i]; } } public function showArgs() { echo 'showArgs() called <br />'; for ($i=0; $i<$numOfArgs; $i++) { echo '$i: ' . $i . '<br />'; echo $this->Arg[$i]; echo '<br />'; } } public function __destruct(){} } $test1 = new variableArgs; $test2 = new variableArgs("arg1"); $test3 = new variableArgs("arg1", "arg2"); $test4 = new variableArgs("arg1", "arg2", "arg3"); $test1->showArgs(); $test2->showArgs(); $test3->showArgs(); $test4->showArgs(); This outputs the following: showArgs() called $i: 0 No arguments passed showArgs() called $i: 0 arg1 showArgs() called $i: 0 arg1 $i: 1 arg2 showArgs() called $i: 0 arg1 $i: 1 arg2 $i: 2 arg3 I have no idea how efficient this is, but it works at any rate. Hopefully this helps someone. ---- Server IP: 66.163.161.117 Probable Submitter: 66.230.112.241 ---- Manual Page -- http://www.php.net/manual/en/language.oop5.decon.php Edit -- https://master.php.net/note/edit/70876 Del: integrated -- https://master.php.net/note/delete/70876/integrated Del: useless -- https://master.php.net/note/delete/70876/useless Del: bad code -- https://master.php.net/note/delete/70876/bad+code Del: spam -- https://master.php.net/note/delete/70876/spam Del: non-english -- https://master.php.net/note/delete/70876/non-english Del: in docs -- https://master.php.net/note/delete/70876/in+docs Del: other reasons-- https://master.php.net/note/delete/70876 Reject -- https://master.php.net/note/reject/70876 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#119309) next »