views:

3480

answers:

6

I was trying to get my Netbeans to autocomplete with PHP, and I learned that this code is valid in PHP:

function blah(Bur $bur) {}

A couple of questions:

  1. Does this actually impose any limits on what type of variable I can pass to the blah method?
  2. If this is just to help the IDE, that's fine with me. How can I declare the type of a variable in PHP if I'm not in a function?
+6  A: 

It's called type hinting, added with PHP 5. It isn't quite what you may be expecting if you are coming from a language like Java. It does cause an error to be thrown if you don't pass in the expected type. You can't type-hint primitives, though (no int $bur).

J Cooper
+4  A: 
  1. Specifying a data type for a function parameter will cause PHP to throw a catchable fatal error if you pass a value which is not of that type. Please note though, you can only specify types for classes, and not primitives such as strings or integers.
  2. Most IDE's can infer a data type from a PHPDoc style comment if one is provided. e.g.

/**
 * @var string
 */
public $variable = "Blah";
Jim OHalloran
I think it does throw an error, not a warning?
J Cooper
what if I'm not in a class? The $variable is coming from an included PHP... I think I'm almost where I need to be...
Yar
And I'll test the warning/error thing tomorrow morning (it's 5am here)...
Yar
No need to test. According to the PHP docs "Failing to satisfy the type hint results in a catchable fatal error." http://ch2.php.net/language.oop5.typehinting
Yar
+1  A: 

Does this actually impose any limits on what type of variable I can pass to the blah method?

This is called type hinting. According to the PHP documentation that I just linked to, yes, it does impose limits on the argument type: "Failing to satisfy the type hint results in a catchable fatal error."

How can I declare the type of a variable in PHP if I'm not in a function?

Read type juggling. You can't explicitly define a variable's type in PHP, its type is decided by the context it is used in.

yjerem
I'm actually just trying to get my IDE to stop juggling and start hinting...
Yar
A: 

Type juggling can be dangerous and any result of it most be doublechecked. I've never come across a situation in PHP where I needed it.

What? First off, this is merely a comment on Jeremy Ruten's answer, so it should be a comment. Secondly, you NEVER use type juggling? You mean, you never say $one = "1"; $two = $one + 3; ????? I seriously doubt that. You probably take user input and make ints out of it...
Yar
+1  A: 

#2 : (...) How can I declare the type of a variable in PHP if I'm not in a function?

I recently heard about "settype()" and "gettype()" in PHP4 & 5
You can force the variable type anytime easily


From PHP.net :

bool settype ( mixed &$var , string $type )

Parameters

var : The variable being converted. type : Possibles values of type are:

  • "boolean" (or, since PHP 4.2.0, "bool")
  • "integer" (or, since PHP 4.2.0, "int")
  • "float" (only possible since PHP 4.2.0, for older versions use the deprecated variant "double")
  • "string"
  • "array"
  • "object"
  • "null" (since PHP 4.2.0)

[ :D First visit, first comment...]

Shad
Yeah I just saw that. settype and gettype are only for primitives, which doesn't help (although technically it does answer the question, so I'll vote you up. Please provide a link to the PHP doc and remove it from your answer).
Yar
+6  A: 

This type-hinting only works for validating function arguments; you can't declare that a PHP variable must always be of a certain type. This means that in your example, $bur must be of type Bur when "blah" is called, but $bur could be reassigned to a non-Bur value inside the function.

Type-hinting only works for class or interface names; you can't declare that an argument must be an integer, for example.

One annoying aspect of PHP's type-hinting, which is different from Java's, is that NULL values aren't allowed. So if you want the option of passing NULL instead of an object, you must remove the type-hint and do something like this at the top of the function:

assert('$bur === NULL || $bur instanceof Bur');
JW
Complete and great answer. I'll admit that my main focus was to get my IDE hinting, which I have already figured out here http://stackoverflow.com/questions/390192... personally, I find that many aspects of PHP are depressing hacks, but that's just my preference for typed langs.
Yar
There there, even those of us who don't prefer strongly-typed languages agree with you :)
J Cooper