Php 强制存储库返回CustomClass类型而不是对象类型

Php 强制存储库返回CustomClass类型而不是对象类型,php,symfony,doctrine,php-7,return-type,Php,Symfony,Doctrine,Php 7,Return Type,在我的Symfony 4、条令2和PHP7.2项目中,我有一个名为Location的实体和一个LocationRepository(从条令的ServiceEntityRepository扩展而来) 我正在实现一个findLocationByUuid方法,其中给定一个字符串,返回一个位置: public function getLocationByUuid(string $uuid): Location { $location = $this->findOneBy(['uuid' =

在我的Symfony 4、条令2和PHP7.2项目中,我有一个名为Location的实体和一个LocationRepository(从条令的ServiceEntityRepository扩展而来)

我正在实现一个findLocationByUuid方法,其中给定一个字符串,返回一个位置:

public function getLocationByUuid(string $uuid): Location
{
    $location = $this->findOneBy(['uuid' => $uuid]);

    if (null == $location) {
        throw new LocationNotFoundException();
    }

    return $location;
}

既然Doctrine的findOneBy方法返回一个对象,那么在方法中严格匹配我的返回类型声明并返回一个位置而不是对象的最佳方式是什么?我是否应该假设此对象与我的位置具有相同的行为,并将对象声明为返回类型?

原则的
findOneBy
是泛型的,因此它不能强制执行
位置
作为返回类型。在您的情况下,我要做的不是检查位置是否为空,而是检查它是否是
location
的实例,因此:

if (!$location instanceof Location) {
    throw new LocationNotFoundException();
}

通过这种方式,您可以确保如果您的方法返回某个内容,那么它是
位置
原则的
findOneBy
是泛型的,因此它不能强制执行
位置
作为返回类型。在您的情况下,我要做的不是检查位置是否为空,而是检查它是否是
location
的实例,因此:

if (!$location instanceof Location) {
    throw new LocationNotFoundException();
}

通过这种方式,您可以确保,如果您的方法返回了某些内容,那么它就是
Location

Symfony 4/bin/console make:entity命令创建实体和存储库类。根据示例,Repository类带有注释的方法,所以您的问题应该得到解决

/**
 * @method TinyPuppy|null find($id, $lockMode = null, $lockVersion = null)
 * @method TinyPuppy|null findOneBy(array $criteria, array $orderBy = null)
 * @method TinyPuppy[]    findAll()
 * @method TinyPuppy[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class TinyPuppyRepository extends ServiceEntityRepository

Symfony 4/bin/console make:entity命令创建实体和存储库类。根据示例,Repository类带有注释的方法,所以您的问题应该得到解决

/**
 * @method TinyPuppy|null find($id, $lockMode = null, $lockVersion = null)
 * @method TinyPuppy|null findOneBy(array $criteria, array $orderBy = null)
 * @method TinyPuppy[]    findAll()
 * @method TinyPuppy[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
 */
class TinyPuppyRepository extends ServiceEntityRepository

LocationRepository::findOneBy将始终返回一个位置,前提是找到一个位置。不用担心它会退回其他东西。这正是条令存储库的工作方式。我想如果你的IDE在抱怨的话,那么你可以一直用打字机来播放它。我可以忽略我的IDE对第二个问题的回答“是”,但对我来说仍然不太好。检查对象的instandeof将是一个解决方案,但它似乎有点无用。在PHP中创建自定义类是非常棘手和肮脏的,我不认为它是干净的。为了安静您的IDE,可以添加<代码> @方法FIDONE(数组$标准,数组$OrdBube = null):位置< /代码>到您的知识库类的DROBACK。实际上,通过类型转换,我的意思是“/**var”位置*//”。它告诉IDE您有一个Location对象。虽然我不明白为什么$location=(location)$this->findOneBy不起作用。但是'@var'应该是您所需要的一切。@Cerad在PHP中只能强制转换为基本类型<代码>$location=(location)$this->findOneBy
无效。LocationRepository::findOneBy将始终返回一个位置,前提是找到一个位置。不用担心它会退回其他东西。这正是条令存储库的工作方式。我想如果你的IDE在抱怨的话,那么你可以一直用打字机来播放它。我可以忽略我的IDE对第二个问题的回答“是”,但对我来说仍然不太好。检查对象的instandeof将是一个解决方案,但它似乎有点无用。在PHP中创建自定义类是非常棘手和肮脏的,我不认为它是干净的。为了安静您的IDE,可以添加<代码> @方法FIDONE(数组$标准,数组$OrdBube = null):位置< /代码>到您的知识库类的DROBACK。实际上,通过类型转换,我的意思是“/**var”位置*//”。它告诉IDE您有一个Location对象。虽然我不明白为什么$location=(location)$this->findOneBy不起作用。但是'@var'应该是您所需要的一切。@Cerad在PHP中只能强制转换为基本类型
$location=(location)$this->findOneBy
无效。doctories
findOneBy
返回指定类型的对象或
null
,因此代码的行为与OPs相同(虽然,与
null
相比,我会使用
==
而不是
==
——但大多数时候这是个人偏好)。理论
findOneBy
要么返回指定类型的对象,要么
null
,因此代码的行为与OPs相同(虽然,与
null
相比,我会使用
==
而不是
==
——但大多数情况下这是个人偏好)。