PHP5 - Datatype of method arguments

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
PhpMachine
Forum Commoner
Posts: 42
Joined: Thu Apr 19, 2007 11:26 am

PHP5 - Datatype of method arguments

Post 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?
Last edited by PhpMachine on Thu Apr 19, 2007 11:48 am, edited 3 times in total.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post 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.
PhpMachine
Forum Commoner
Posts: 42
Joined: Thu Apr 19, 2007 11:26 am

Post 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
PhpMachine
Forum Commoner
Posts: 42
Joined: Thu Apr 19, 2007 11:26 am

Post by PhpMachine »

Anyone?
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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.
Post Reply