php
PHP Types and Typehints

PHP Types and Typehints

This article will have a look at types and their type hints. For the type hint section you need to have php7.1 or greater.

Types

In php there are two types of data types. Primitive and complex data types. Primitive data types can only hold one value at a time, like an integer. On the other side we have complex types which are a composition of other types. Like for example an array is composed of it's individual elements.

Basic types

  • string: List of characters
  • boolean: Either true or false

Number

  • int: For whole number
  • float: For floating point numbers

Division is contrary to other languages both integer and floating point division. If the result is an integer like 4/2=2 the result will be of type int. If not then it is float 5/2 = 2.5.

Arrays

Arrays in PHP are strange things but basically lists. Most programming languages differentiate between arrays where the index (way of addressing elements in the list) is sequential e.g. 0, 1, 2, 3, 4 and non sequential like:
$translations = ['en' => 'Hello', 'de' => 'Hallo', 'fr' => 'Bonjour'];

Accessing elements works like this: echo $translations['en'];
But watch out it is not type safe. Because the element $a[0] and $a['0'] are the same.

Objects

Objects are similar to arrays. However they can be created easier and make multiple instances with the same structure easy to maintain.

class A{
    public $name;
    public $age;
}
$a = new A();

Now the variable $a can hold a name in $a->name and an age in $a->age at the same type.

Typehints

Typehints are is a way for the programmer to specify what type of arguments a function take or what type of property should have. For example we can tell in a comment the PHP editor that $name should be a string and $age an int.

class A{
    /** @var string */
    public $name;

    /** @var int */
    public $age;
}

For functions in PHP this is quite a little more subtle:

function printPerson(A $person): void {
    echo $person->name.' is '. $person->age;
}

This will ensure that an instance of class A is passed into it so $name and $age will (most likely, yes you can remove attributes but just don't) have. After the colon you can also optionally add a return type (void = nothing gets returned). But if you don't turn on strict types PHP will behind your back still accept types that are implicitly convertible:

function printName(string $a): void {
    echo $a;
}
printName(10);

To denote that a type can also be null you just put a question mark in front of it:

function divide(int $a, ?int $b):float {
    if($b === null) { $b = 1; }
    return (float) ($a/$b);
}

If you want to modify input values of the function (which is normally not a good idea) you can do so.

function addOne(int &$x): void {
    $x+=1;
}

function addNone(int $x): void {
    $x+=1;
}

$a = 5; 
addOne($a);
//$a will be 6 because the value at $a was modified

addNone($a);
//$a will still be 6 because the variable was copied into the function

In this case we have two more or less identical functions in addOne we take the reference and give it to the function. That means you can use this variable and write to it with it affecting the variable that went in. And in addNone we give the function a copy of the value passed in so that only the local variable is modified but the outside is not touched.

Final words

PHP is not really type-safe there is no native support for types for object properties (only through making it private and exposing it through getter and setter). Arrays are still a bunch of different values and there is no way to specify that a function only takes an array of integers (only with doc types). We probably have to wait still some time for that.

# PHP Types and Typehints This article will have a look at types and their type hints. For the type hint section you need to have php7.1 or greater. ## Types In php there are two types of data types. Primitive and [complex](https://www.ibm.com/support/knowledgecenter/en/SSGU8G_11.50.0/com.ibm.ddi.doc/ids_ddi_313.htm) data types. Primitive data types can only hold one value at a time, like an integer. On the other side we have complex types which are a composition of other types. Like for example an array is composed of it's individual elements. ### Basic types * string: List of characters * boolean: Either true or false ### Number * int: For whole number * float: For floating point numbers Division is contrary to other languages both integer and floating point division. If the result is an integer like `4/2=2` the result will be of type `int`. If not then it is float `5/2 = 2.5`. ### Arrays Arrays in PHP are strange things but basically lists. Most programming languages differentiate between arrays where the index (way of addressing elements in the list) is sequential e.g. 0, 1, 2, 3, 4 and non sequential like: `$translations = ['en' => 'Hello', 'de' => 'Hallo', 'fr' => 'Bonjour'];` Accessing elements works like this: `echo $translations['en'];` But watch out it is not type safe. Because the element `$a[0]` and `$a['0']` are the same. ### Objects Objects are similar to arrays. However they can be created easier and make multiple instances with the same structure easy to maintain. ```php class A{ public $name; public $age; } $a = new A(); ``` Now the variable $a can hold a `name` in `$a->name` and an age in `$a->age` at the same type. ## Typehints [Typehints](http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration) are is a way for the programmer to specify what type of arguments a function take or what type of property **should** have. For example we can tell in a comment the PHP editor that `$name` should be a string and `$age` an int. ```php class A{ /** @var string */ public $name; /** @var int */ public $age; } ``` For functions in PHP this is quite a little more subtle: ```php function printPerson(A $person): void { echo $person->name.' is '. $person->age; } ``` This will ensure that an instance of class A is passed into it so `$name` and `$age` will (most likely, yes you can remove attributes but just don't) have. After the colon you can also optionally add a return type (void = nothing gets returned). But if you don't turn on strict types PHP will behind your back still accept types that are implicitly convertible: ```php function printName(string $a): void { echo $a; } printName(10); ``` To denote that a type can also be null you just put a question mark in front of it: ```php function divide(int $a, ?int $b):float { if($b === null) { $b = 1; } return (float) ($a/$b); } ``` If you want to modify input values of the function (which is normally not a good idea) you can do so. ```php function addOne(int &$x): void { $x+=1; } function addNone(int $x): void { $x+=1; } $a = 5; addOne($a); //$a will be 6 because the value at $a was modified addNone($a); //$a will still be 6 because the variable was copied into the function ``` In this case we have two more or less identical functions in `addOne` we take the reference and give it to the function. That means you can use this variable and write to it with it affecting the variable that went in. And in `addNone` we give the function a copy of the value passed in so that only the local variable is modified but the outside is not touched. ## Final words PHP is not really type-safe there is no native support for types for object properties (only through making it private and exposing it through getter and setter). Arrays are still a bunch of different values and there is no way to specify that a function only takes an array of integers (only with doc types). We probably have to wait still some time for that.

Hello

3
199
0
1
live preview
enter atleast 10 characters
WARNING: You mentioned %MENTIONS%, but they cannot see this message and will not be notified
Saving...
Saved
With selected deselect posts show selected posts
All posts under this topic will be deleted ?
Pending draft ... Click to resume editing
Discard draft