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?

14
  • 2
    PHP doesn't support hinting for simple types - only named classes and interfaces, stdClass, array and (latest version) callable. Commented Feb 2, 2013 at 17:59
  • Unrelated, but you probably want those attributes changed from public to protected, so they are encapsulated by the class. Commented Feb 2, 2013 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.. Commented Feb 2, 2013 at 18:07
  • 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! Commented Feb 2, 2013 at 18:26
  • 1
    That Google article is sure to be right in performance terms, but for large systems that isn't great advice, imo. Commented Feb 2, 2013 at 18:31

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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.

Comments

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.