简体   繁体   English

Typo3 extbase @param int无效

[英]Typo3 extbase @param int not working

I got this function in my ProductsRepository: 我在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: 在使用PHP 5.6.3的Typo3 6.2.6中,这很好,但在Typo3 6.2.12和PHP 5.6.12中,它在我的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 [Wed Sep 09 15:59:32.922153 2015] [:error] [pid 26601] [client 192.168.113.4:58686] PHP Catchable致命错误:参数1传递给Foo \\ Bar \\ Domain \\ Repository \\ ProductsRepository :: getProductById()必须是Foo \\ Bar \\ Domain \\ Repository \\ int的实例,给定整数

Do you know why my repository wants Foo\\Bar\\Domain\\Repository\\int instead of int ? 你知道为什么我的存储库想要Foo\\Bar\\Domain\\Repository\\int而不是int吗?

As mentioned in comments by Jost and Viktor, this is not a TYPO3 error message, but a PHP error message. 正如Jost和Viktor的评论中所提到的,这不是TYPO3错误消息,而是PHP错误消息。 PHP does not support type hints for primitive types like int , bool and the like (it will, as of PHP 7 ). PHP不支持基本类型的类型提示,如intbool等( 从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). 这段代码实际上做了什么 :通过使用int作为类型提示,PHP假定您需要一个类int的实例作为参数(请注意,此行为将在PHP 7中更改)。 Because no namespace is specified, PHP assumes this int class to be in your current namespace Foo\\Bar\\Domain\\Repository . 由于未指定任何名称空间,因此PHP假定此int类位于当前名称空间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->productRepository->getProductById(5)调用此方法时,您将传递一个整数值( 5 )作为参数,其中PHP需要一个(可能不存在的)类Foo\\Bar\\Domain\\Repository\\int的实例Foo\\Bar\\Domain\\Repository\\int This then triggers the "Argument 1 passed to ... must be an instance of ..., integer given" error. 然后,这会触发“传递给...的参数1必须是...,给定整数”的错误。

To summarize: Using scalar type hints has never worked and will never work in PHP 5 (it will, in PHP 7). 总结一下:使用标量类型提示从未起作用,并且永远不会在PHP 5中工作(它将在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. 如果它似乎确实在某些时候起作用,很可能它确实没有用,但是由于PHP中可能已经修复的错误而没有触发错误。

Going back to TYPO3: In specific places, you can use scalar type hints in annotations. 回到TYPO3:在特定的地方,您可以在注释中使用标量类型提示。 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) . 但是,这只能在请求参数映射到方法参数的位置才能实现(最值得注意的是控制器操作,您可以使用@param注释和域实体属性,您可以使用@var注释。这是一个框架功能,但是,并且不会对$this->productRepository->getProductById(5)等常规方法调用起作用。

Try @param \\integer instead an remove the type hinting in the arguments. 尝试@param \\integer而不是删除参数中的类型提示。 However, the type \\integer is what the Extension Builder generates by default in model classes. 但是,类型\\integer是Extension Builder默认在模型类中生成的内容。

I recommend using only the PHPDoc annotations instead of PHPs Type Hinting. 我建议仅使用PHPDoc注释而不是PHP类型提示。 Extbase validates the type where it is needed and you get in less trouble. Extbase验证了需要它的类型,你可以减少麻烦。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM