Defining argument types of multiple functions in PHP

Is it possible to define a function argument as several possible types? For example, a function may take a string or an integer to do something. Is there any way to define it like that?

    function something(int|string $token) {}

Or is only one type supported for an argument?

(note, I know that I can filter the input later, I just like my arguments)

+5
source share
3 answers

The best you can do is called Type Hinting and is explained here:

http://php.net/manual/en/language.oop5.typehinting.php

, , ( ) " hinting int string ". , , , .

.. , .

+2

, .

, hinting PHP 5 . http://php.net/manual/en/language.oop5.typehinting.php

class Foo
{
}

function something(Foo $Object){}
+3

PHP , hinting, , , , :

function something($token) 
{
   if (is_numeric($token)) {
     // its a float, double, integer

   } else {
     // its a string, array, object
   }
}

( , ).

, , (IMHO) - , PHP

0

All Articles