8

I just got this Fatal Error

Catchable fatal error: Argument 1 passed to File::__construct() must be an instance of integer, integer given, 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 the 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 triggers the error

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

Am I missing something or there is something bad with this PHP type hinting?

Michael
  • 4,786
  • 11
  • 45
  • 68
  • 2
    PHP doesn't support hinting for simple types - only named classes and interfaces, `stdClass`, `array` and (latest version) `callable`. – halfer Feb 02 '13 at 17:59
  • Unrelated, but you probably want those attributes changed from `public` to `protected`, so they are encapsulated by the class. – halfer Feb 02 '13 at 18:02
  • Oh, that's not great.. About the public properties - I do not intend to create setters and this way I can change an instance's state easily.. – Michael Feb 02 '13 at 18:07
  • _this way I can change an instance's state easily_ - that rather defeats one of the great things about classes. "Encapsulation" means that you hide the internals of a class from things outside of it, and only your public methods are used as well-defined entry points to the class's functionality. Setters are, therefore, recommended for this reason. – halfer Feb 02 '13 at 18:11
  • I'd like to see trivial type-hinting in PHP as well, but it has been specifically decided against (see the mailing list for why, I don't know the reasons). In practice though, it doesn't cause me a great deal of problems - program defensively and you'll be fine. I use the class, interface and array hints all the time. – halfer Feb 02 '13 at 18:12
  • Related: http://stackoverflow.com/questions/8318238/php-string-type-hinting and http://stackoverflow.com/questions/4754318/why-is-traditional-type-hinting-not-allowed-in-php – halfer Feb 02 '13 at 18:18
  • The comment from the suggested related link is good :). But, regarding encapsulation, in this very simple particular case, do you really think is absolutely necessary to create getters and setters while there is no kind of data proccesing or something like that? – Michael Feb 02 '13 at 18:22
  • By the way, you should have answered the question - you were the first to clarify things. – Michael Feb 02 '13 at 18:24
  • 1
    It's difficult to answer about encapsulation without seeing the whole class and how significantly it is (and will be) used. It will certainly work, but my view is always to start with as good a design as possible - the danger is you'll start by saying "public attributes are fine for a small class", and then it will grow into a large one, and you'll end up with _technical debt_ that will be harder to maintain. But, it is up to you! – halfer Feb 02 '13 at 18:26
  • Ah it's okay, give the tick to @Ynhockey `:)`. – halfer Feb 02 '13 at 18:26
  • Look, while I was searching for some optimisation hints, I found this https://developers.google.com/speed/articles/optimizing-php - where it is suggested not to use "naive" setters and getters. – Michael Feb 02 '13 at 18:27
  • +1 for the long perspective view approach. – Michael Feb 02 '13 at 18:28
  • 1
    That Google article is sure to be right in performance terms, but for large systems that isn't great advice, imo. – halfer Feb 02 '13 at 18:31
  • I recommend to change the tag from php to php 5, or just add php5 – Joako-stackoverflow Sep 26 '17 at 11:59

3 Answers3

4

Since this could be misleading and since this answer is still quite high in search engines.

PHP 7 did introduce type hinting for scalar types

There is no scalar type hinting in PHP 5, so the integer type hint is considered to be a class type hint.

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

Finwe
  • 6,372
  • 2
  • 29
  • 44
Joako-stackoverflow
  • 506
  • 1
  • 6
  • 16
3

You can't force a parameter to be an integer.

Look here language.oop5.typehinting :

PHP 5 introduces type hinting. Functions are now able to force parameters to be objects [...], interfaces, arrays (since PHP 5.1) or callable (since PHP 5.4).

[...]

Type hints can not be used with scalar types such as int or string. [...]

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

- boolean
- integer
- float (floating-point number, aka double)
- string
Eric Lavoie
  • 5,121
  • 3
  • 32
  • 49
2

As far as I know, you can't use the integer type hint in PHP. However, someone in PHP.net had this helpful comment:

http://www.php.net/manual/en/language.oop5.typehinting.php#83442

It's apparently a workaround that will work for you if you really need this functionality.

GPHemsley
  • 535
  • 1
  • 7
  • 18
Ynhockey
  • 3,845
  • 5
  • 33
  • 51