Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring Variable Types in PHP?

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?
like image 700
Dan Rosenstark Avatar asked Dec 24 '08 06:12

Dan Rosenstark


People also ask

Can we declare data types in PHP?

The values assigned to a PHP variable may be of different data types including simple string and numeric types to more complex data types like arrays and objects. PHP supports total eight primitive data types: Integer, Floating point number or Float, String, Booleans, Array, Object, resource and NULL.

What is type declaration in PHP?

Type declarations can be added to function arguments, return values, and, as of PHP 7.4. 0, class properties. They ensure that the value is of the specified type at call time, otherwise a TypeError is thrown. Note: When overriding a parent method, the child's method must match any return type declaration on the parent.


2 Answers

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'); 

EDIT: This last paragraph doesn't apply since PHP 5.1; you can now use NULL as a default value, even with a type hint.

EDIT: You can also install the SPL Type Handling extension, which gives you wrapper types for strings, ints, floats, booleans, and enums.

EDIT: You can also use "array" since PHP 5.1, and "callable" since PHP 5.4.

EDIT: You can also use "string", "int", "float" and "bool" since PHP 7.0.

like image 176
JW. Avatar answered Sep 30 '22 15:09

JW.


  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"; 

UPDATE 2021: As of PHP 7 (which is several years old at this point) primitive types can also be declared for function arguments. Nullability can also be indicated with a ? in front of the type from 7.1 onward. You can declare return types now too. So this is valid PHP these days:

public function hasFoo(?int $numFoos) :bool { 

phpStorm (my current preferred IDE) is happy to use all of these types for code completion, so I don't need as many phpDoc comments for typing as I used to.

like image 35
Jim OHalloran Avatar answered Sep 30 '22 14:09

Jim OHalloran