PHP type hint error?

I just got this fatal error

Acceptable fatal error: argument 1 passed to :: __ construct () must be an integer instance called in / home / radu / php _projects / audio_player / index.php on line 9 and defined in / home / radu / php _projects / audio_player / php / File.php on line 7

So there is a class

class File{        
        public $id;
        public $name;
        public $file_paths;
        public function __construct(integer $id=null, string $name=null, array $file_paths=null)
        {
            foreach(func_get_args() as $name => $val)
            {
                $this->$name = $val;
            }
        }
    }

And here is the code that causes the error

$file = new File(1, "sound", array());

Am I missing something or is something bad with this hint of a PHP type?

+5
source share
3 answers

Since this can be misleading, and since this answer is still pretty high in search engines.

PHP 7 did introduce type hints for scalar types

PHP 5 , integer .

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

+2
+2

.

language.oop5.typehinting:

PHP 5 . [...], , ( PHP 5.1) ( PHP 5.4).

[...]

, int string. [...]

And here language.types.intro , PHP scalar types:

- boolean
- integer
- float (floating-point number, aka double)
- string
+1
source

All Articles