php: Declare arguments type of a Function-ThrowExceptions

Exception or error:

I’m trying to make a function with declared argument types, to quickly check if they are in the right format, but when it returns a string I this error:

Catchable fatal error: Argument 2 passed to myfunction() must be an instance of string, string given, called in path_to_file on line 69 and defined in path_to_file on line 49

Example

function myfunction( array $ARRAY, string $STRING, int $INTEGER ) { 
    return "Args format correct"; 
}
myfunction(array("1",'2','3','4'), "test" , 1234);

Where is the mistake?

How to solve:

According to the PHP5 documentation:

Type Hints can only be of the object and array (since PHP 5.1) type. Traditional type hinting with int and string isn’t supported.

Since string and int are not classes, you can’t “type-hint” them in your function.

As of PHP 7.0 declaring argument type as string, int, float, bool is supported.

Answer:

This maybe useful for anyone who see this post since the availability of PHP 7

With PHP 7, its now possible to declare types. You can refer the following link for more information.

http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration

function(string $name, boolean $is_admin) {
    //do something
}

Answer:

You can do something like this which has always worked for me

for string

function setData($Name=""){ }

this forces the name to be a string, it doesn’t check if its a string

for numeric values

function setData($age=0){ }

this forces the age to be a number, if a string is passed, the value will be 0

for array values , there are two variation

function setData(array $data){ } 

if an array is not passed, it would throw an error

function setData($data=array()){ } 

This would pass an empty array of no value is given for $data

Answer:

According to PHP Manual, you can do that for array on PHP 5.1 and beyond and for string and int types on PHP 7 and beyond. Take a look:

  • Class/interface name The parameter must be an instanceof the given class or interface name. PHP 5.0.0
  • self The parameter must be an instanceof the same class as the one the method is defined on. This can only be used on class and instance methods. PHP 5.0.0
  • array The parameter must be an array. PHP 5.1.0

  • callable The parameter must be a valid callable. PHP 5.4.0
  • bool The parameter must be a boolean value. PHP 7.0.0
  • float The parameter must be a floating point number. PHP 7.0.0
  • int The parameter must be an integer. PHP 7.0.0

  • string The parameter must be a string. PHP 7.0.0

  • iterable The parameter must be either an array or an instanceof Traversable. PHP 7.1.0

Answer:

string, int and other built-in types are not classes, in argument you specify class, of the argument. The only supported built-in type to be put there is array.

Answer:

You cannot define type as string and int. PHP “does not know” what they are.

Answer:

If you are not using PHP 7.x or need some complex argument validations (like “an array or \Traversable” because you can traverse arrays but they are primitive types and don’t implement the \Traversable interface) you can use args module from Non-standard PHP library (NSPL).

use const \nspl\args\numeric;
use function \nspl\args\expects;

function sqr($x)
{
    expects(numeric, $x);
    return $x * $x;
}

sqr('hello world');

Outputs:

InvalidArgumentException: Argument 1 passed to sqr() must be numeric, string given in /path/to/example.php on line 17

Call Stack:
    0.0002     230304   1. {main}() /path/to/example.php:0
    0.0023     556800   2. sqr() /path/to/example.php:17

Answer:

when I need use Type Hints, I do this:

<?php 
declare(strict_types=1);

Leave a Reply

Your email address will not be published. Required fields are marked *