Archive for September 3rd, 2007

Methods with unknown number of parameters

September 3, 2007

Recently I am studying “Essential Actionscript 3.0″ by Colin Mook. I am in the first chapter itself and informations are already there to digest. I simply love the Mook’s books. Previously it was for AS2 and that was too good, now this is an excellent addition. This writing simply brings the books pages to here with my own edits :) .
Ok, straight to the point, how to write a method,  inside a class, which can handle “multiple” or better word as per Mook is “unknown” number of parametrs ?!! The answer is “…(rest)” parameter. This parameter defines an Array to hold any number of arguments passed to a given method. It can be used on its own or in combination with named parameters.
Using the parameter alone :
function myFun(…what){
for(var i:int=0;i<what.length;i++){
trace(what[i]);
}
}
myFun(’a',’c',’b',’g',’h',’j');

Now try to call the function with some new number of parameters as
myFun(’a',’c',’b',’g');
The result will be obviuos.Lets try to use the “…(rest)” parameter with a named parameter.
function samAdd(a:int,…more){
var addValue:int=a;
for(var j:int;j<more.length;j++){
addValue+=more[j];
}
trace(’sum=’+addValue);
}
samAdd(5,4,5);

I have simply added “sam” before the function name, instead of my name “saumya” :) . Ok, now if you look at the result it will give you “14″.Lets the call the function with the desired number of parameter, which is 1.I mean you must pass one parameter to successfully call this function.So the next call will be
samAdd(20);
This will result in “20″, but what happens if we call it without any parameter as
samAdd();
The compiler throws an error saying “Incorrect number of arguments.Expected 1.”

When used with other parameters, the “…(rest)” parameter must be the last parameter in the parameter list.That means, we can not write
function samAdd(…more,a:int){
//code to be executed
}

For me, its just WOW.

Cheers to Mook. I will put things like this as i proceed through the book.

PHP 5.2.4 Released and PHP 4 end of life announcement is done

September 3, 2007

There is a new version of PHP released and at the same time the death of PHP 4 is also announced. There are a number of new enhancements to the new release of PHP 5.2.4. For a complete list of enhancements and bug fixes, you must have a look at this.