24

Is it possible to define a function argument as multiple possible types? For example, a function may take a string or an integer to accomplish something. Is it possible to somehow define it like this?

    function something(int|string $token) {}

Or is only one type per argument supported?

(mind you, I know I can filter input later on, I just like to have my arguments typed)

1

4 Answers 4

47

2020 Update:

Union types have finally been implemented in PHP 8.0, which is due for release near the end of 2020.

They can be used like this:

class Number {
    private int|float $number;

    public function setNumber(int|float $number): void {
        $this->number = $number;
    }

    public function getNumber(): int|float {
        return $this->number;
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Awesome, with PHP 8 becoming the norm, this would be a useful tool to have.
While it's not part of the question, it would be useful to say here if they're also ok for return types or not.
3

No, it is not possible.

Also, type hinting in PHP 5 is now only for classes and arrays. http://php.net/manual/en/language.oop5.typehinting.php

class Foo
{
}

function something(Foo $Object){}

Comments

2

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

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

In particular, you can hint a class type or an array type, but (as the manual says) "Traditional type hinting with int and string isn't supported." So I guess that what you are trying to accomplish is not possible at this level.

However, you can create your own wrappers, etc. There are probably a thousand ways to handle this.

2 Comments

Cheers, I finally know it's called Type Hinting now.
It's called Hinting in PHP (only?) because it is in fact just a hint for the PHP interpreter, which is thus instructed to "artificially" check the argument type and throw an error if it does not match with the hint. In statically typed languages it is a basic and required thing, part of the syntax for functions, and it is not called like that... in fact I don't think it has a specific name otherwise...
0

PHP is dynamically typed - but type hinting has been added to the language - so if you've not hinted the parameter you can pass any type you like:

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

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

(off the top of my head I'm not sure how resources are handled).

However if you want to program in a strongly typed language, then (IMHO) you should be using something other than PHP

1 Comment

is_numeric also includes numeric strings, but not resources.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.