3

I got this function in my ProductsRepository:

/**
 * @param int $ProductId
 * @return \Foo\Bar\Domain\Model\Products
 */
public function getProductById(int $ProductId) {
    $query = $this->createQuery();
    $query->matching(
        $query->equals('productId', $ProductId)
    );
    $query->setLimit(1);
    return $query->execute()->getFirst();
}

which I call like this:

$product = $this->productsRepository->getProductById($ProductId);

In Typo3 6.2.6 with PHP 5.6.3 this worked fine, but in Typo3 6.2.12 with PHP 5.6.12 it says the following in my error.log:

[Wed Sep 09 15:59:32.922153 2015] [:error] [pid 26601] [client 192.168.113.4:58686] PHP Catchable fatal error: Argument 1 passed to Foo\Bar\Domain\Repository\ProductsRepository::getProductById() must be an instance of Foo\Bar\Domain\Repository\int, integer given

Do you know why my repository wants Foo\Bar\Domain\Repository\int instead of int?

helmbert
  • 35,797
  • 13
  • 82
  • 95
Marcel
  • 627
  • 7
  • 25
  • It seems PHP does stricter checking of type hints? The error message is from PHP, not TYPO3. Try to remove the `int` from `getProductById(int $ProductId)`. – Jost Sep 09 '15 at 17:59
  • Yes, I know thats an error message from PHP. It works, if I remove the int, but I think this is not the best solution. – Marcel Sep 10 '15 at 07:01
  • Did you try to add leading slash, like `\int`? – Viktor Livakivskyi Sep 10 '15 at 07:50
  • And just for information: scalar type declarations are available only from [PHP 7](http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration.types), so it actually shouldn't work. – Viktor Livakivskyi Sep 10 '15 at 07:55
  • Nope, not working. But in PHP 5.6.3 with the older Typo3 version this worked. – Marcel Sep 10 '15 at 08:06
  • So, something was changed in-between PHP 5.6.3 and 5.6.12. The only recommendation is to remove scalar type declaration, because it is not supported by PHP 5.6. – Viktor Livakivskyi Sep 10 '15 at 11:33

2 Answers2

3

As mentioned in comments by Jost and Viktor, this is not a TYPO3 error message, but a PHP error message. PHP does not support type hints for primitive types like int, bool and the like (it will, as of PHP 7).

What this code actually does: By using int as type hint, PHP assumes that you need an instance of a class int as an argument (note that this behaviour will change in PHP 7). Because no namespace is specified, PHP assumes this int class to be in your current namespace Foo\Bar\Domain\Repository. This means when you call this method using $this->productRepository->getProductById(5), you're passing an integer value (5) as parameter, where PHP expects an instance of the (probably non-existent) class Foo\Bar\Domain\Repository\int. This then triggers the "Argument 1 passed to ... must be an instance of ..., integer given" error.

To summarize: Using scalar type hints has never worked and will never work in PHP 5 (it will, in PHP 7). If it did appear to work at some point, chances are that it did not really work, but no error was triggered due to a bug in PHP that might have been fixed between versions.

Going back to TYPO3: In specific places, you can use scalar type hints in annotations. However, this is only possible in places where request parameters are mapped on method arguments (most notably controller actions in which you can use a @param annotation and domain entity attributes where you can use a @var annotation. This is a framework feature though, and will not work on regular method calls like $this->productRepository->getProductById(5).

helmbert
  • 35,797
  • 13
  • 82
  • 95
0

Try @param \integer instead an remove the type hinting in the arguments. However, the type \integer is what the Extension Builder generates by default in model classes.

I recommend using only the PHPDoc annotations instead of PHPs Type Hinting. Extbase validates the type where it is needed and you get in less trouble.

Jay Dinse
  • 504
  • 4
  • 19