Page 1 of 1

PHP5 - Datatype of method arguments

Posted: Thu Apr 19, 2007 11:35 am
by PhpMachine
Hi

I have problems defining datatypes for my method arguments (PHP 5.2.1).
I have this piece of code:

Code: Select all

<?php
myclass::test(1);

class myclass {
 public static function test(int $number) {
  echo "success";
 }
}
?>
When I run that code, I get this error:
"Catchable fatal error: Argument 1 passed to myclass::test() must be an instance of int, integer given, called in test.php on line 2 and defined in test3.php on line 5"

What is wrong?
string doesn't work either.

Isn't it possible to set datatypes in PHP5?

Posted: Thu Apr 19, 2007 11:41 am
by volka
http://de2.php.net/manual/en/language.oop5.typehinting.php wrote:PHP 5 introduces Type Hinting. Functions are now able to force parameters to be objects (by specifying the name of the class in the function prototype) or arrays (since PHP 5.1).
[...]
Traditional type hinting with int and string isn't supported.

Posted: Thu Apr 19, 2007 11:48 am
by PhpMachine
Thanks for a fast reply! :)

Ohh, so "int" and "string" isn't supported.
That's very bad...

Im using Zend Studio, and when I comment the function above, I type:

Code: Select all

/**
and hit <enter>.

Then I automatically get this:

Code: Select all

/**
  * Enter description here...
  *
  * @param unknown_type $number
  */
Now it says "unknown_type".
Do I manually have to edit this to "int", "string"?

I have several hundreds of methods, and even more argument to edit if so.
So, is there a solution to this?

Greetings

Posted: Fri Apr 20, 2007 7:32 am
by PhpMachine
Anyone?

Posted: Fri Apr 20, 2007 7:50 am
by Oren
Yes, you need to manually type whatever you expect it to be, but note that this is just a comment and in PHP you don't declare for each variable what type it is. You can assign to the same variable a string, int, float, arrays, objects... whatever you want.