Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.
function PrintString(string $string)
{
print($string);
}
PrintString(9);
Outputs:
Fatal Error: Arguement 1 passed to PrintString() must be an object of class string, called in __FILE__ on line __LINE__ and defined in __FILE__ on line __LINE__
I had no idea that you could do that in PHP (I don't know what that is called btw), but I did, in PHP5.
It's sweet that PHP can go both ways because I hate having to declare what kind of thingy a variable is most of the time, but sometimes I really wish I could.
I know that it's like that in most languages, but I swear the other day someone said you can't do it in PHP.
So, this morning on the bus-ride to work I was working on my error handling class (which is going to be hawt when it's done btw) and I forgot the $ in front of my variable named object, and it flashed colors (i use dreamweaver) and i was like omg you are kidding.
Its not however a very desireable way of type checking , as it throw's a fatal error from which your script cannot recover. Only use it when your class can not function at all unless the parameter is a certain type, or an instance of a certain object.
class MyClass
{
public function __construct ($var)
{
if (is_string($var)) {
//do something with string..
} elseif (is_int($var)) {
//do something with int..
}
}
}
Last edited by Jenk on Wed Jul 26, 2006 3:49 pm, edited 1 time in total.
The function name is the same, but the function signature (functionName@argument1type, for instance) is different. In languages that support such functionality, if I called