Php Laravel:当在控制器中使用两次时,类型暗示类的奇怪行为
在Laravel控制器中使用类型暗示时,我遇到了一个我不理解的奇怪行为。我有一个配置文件控制器,它应该根据不同的参数调用服务的相同函数两次。但是,当我第二次调用该函数时,第一次调用的结果会改变 这是我的密码;应在一个页面上显示两个用户配置文件:Php Laravel:当在控制器中使用两次时,类型暗示类的奇怪行为,php,laravel,laravel-5,type-hinting,Php,Laravel,Laravel 5,Type Hinting,在Laravel控制器中使用类型暗示时,我遇到了一个我不理解的奇怪行为。我有一个配置文件控制器,它应该根据不同的参数调用服务的相同函数两次。但是,当我第二次调用该函数时,第一次调用的结果会改变 这是我的密码;应在一个页面上显示两个用户配置文件: //use statements are omitted for brevity class ProfileController extends Controller { protected $compose_profile_service;
//use statements are omitted for brevity
class ProfileController extends Controller {
protected $compose_profile_service;
public function __construct(ComposeProfileService $compose_profile_service) {
$this->compose_profile_service = $compose_profile_service;
}
public function compare($user1, $user2) {
$user1_profile = $this->compose_profile_service->composeProfile($user1);
$user2_profile = $this->compose_profile_service->composeProfile($user2);
dd($user1_profile); //Problem: This dd() gives me the profile of user2 instead of user1
}
}
class ComposeProfileService {
public function composeProfile($user) {
//Get some stuff from the DB
$profile_data = $user->profile()->get();
return $profile_data;
}
}
当然,如果在compose函数的相应调用上方添加了dd()$user1或$user2,则会提供正确的用户 如果您在调用compose on
$user2
之前dd($user1\u profile)
,它是正确的配置文件吗?如果您有一个服务提供商构造了ComposeProfileService
实例,您能分享它是如何构造的吗?@Jeff yes!“它是正确的。@watcher:据我所知,它是使用Laravel的IOC容器@JoschiVzfam构建的。如果一个对象的构建需要其他对象,那么您必须使用服务提供商告诉IOC如何创建它。我只是问你是否有任何奇怪的东西在一个服务提供商里面发生如果你在调用compose on$user2
之前dd($user1\u profile)
它是正确的配置文件吗?如果你有一个构建ComposeProfileService
实例的服务提供商,你能分享它是如何构建的吗?@Jeff yes!“它是正确的。@watcher:据我所知,它是使用Laravel的IOC容器@JoschiVzfam构建的。如果一个对象的构建需要其他对象,那么您必须使用服务提供商告诉IOC如何创建它。我只是问你在服务提供商里面有没有什么奇怪的事情