EntityManager::find return type is just object in the lexer. #5782

Closed
opened 2026-01-22 15:17:39 +01:00 by admin · 3 comments
Owner

Originally created by @axelcho on GitHub (Nov 22, 2017).

Originally assigned to: @Ocramius on GitHub.

I would like to force strict class check on find return. It is theoretically possible to return anything as a result of EntityManger::find

Also, on EntityManager::persist($entity), there is no type hint on $entity, and theoretically I can pass any object to the entity.

Originally created by @axelcho on GitHub (Nov 22, 2017). Originally assigned to: @Ocramius on GitHub. I would like to force strict class check on find return. It is theoretically possible to return anything as a result of EntityManger::find Also, on EntityManager::persist($entity), there is no type hint on $entity, and theoretically I can pass any object to the entity.
admin added the InvalidQuestion labels 2026-01-22 15:17:39 +01:00
admin closed this issue 2026-01-22 15:17:43 +01:00
Author
Owner

@coudenysj commented on GitHub (Nov 22, 2017):

A good solution is to use Repositories (http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#custom-repositories) for that.

You can implement a "find" method there (which is basically the same as doing it in the EM).

As for the persist type hint, you could add a "save" method to the Repository with a type hint.

@coudenysj commented on GitHub (Nov 22, 2017): A good solution is to use Repositories (http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#custom-repositories) for that. You can implement a "find" method there (which is basically the same as doing it in the EM). As for the persist type hint, you could add a "save" method to the Repository with a type hint.
Author
Owner

@Ocramius commented on GitHub (Nov 22, 2017):

@axelcho this is because the ORM actually accepts any object, and will fail only when the given object is not mapped.

This is by design, as PHP has no better type system to work with.

Closing as invalid

@Ocramius commented on GitHub (Nov 22, 2017): @axelcho this is because the ORM actually accepts any object, and will fail only when the given object is not mapped. This is by design, as PHP has no better type system to work with. Closing as `invalid`
Author
Owner

@Majkl578 commented on GitHub (Dec 6, 2017):

Note: This will be improved by adding native object type in develop/3.0 where we already require PHP 7.2. Siínce PHP has no generics, nothing better could be done here.

@Majkl578 commented on GitHub (Dec 6, 2017): Note: This will be improved by adding native `object` type in develop/3.0 where we already require PHP 7.2. Siínce PHP has no generics, nothing better could be done here.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5782