php - Laravel Dependency Injection, using setting function -


firstly apologize if it's duplicated or repeating question.

i'm "new" dependency injection. understood how in __constructor functions not sure setter function. wanna use di correctly, question is: using right way in following code:

public function setnotification(notification $notification) {     $this->notification = $notification; }  public function handle(postwascommented $event) {     $post = $event->comment->post;      $post->load('blog');      $followers = $post->followers;      $online_ids = redis::pipeline(function($pipe) use ($event, $followers, $post)     {         foreach($followers $follower){             if(auth::id() != $follower->user_id){                 $this->setnotification(new notification());                 $this->notification->from_id = auth::id();                 $this->notification->to_id = $follower->user_id;                 $this->notification->type = 'postcomment';                 $this->notification->source_id = $event->comment->id;                 $this->notification->parent_id = $event->comment->post_id;                 $this->notification->blog_name = $post->blog->link_name;                  if($post->user_id == $follower->user_id)                     $this->notification->my_post = true;                 else                     $this->notification->my_post = false;                  $this->notification->save();                  $pipe->get('user'.$follower->user_id);             }         }     });  

i found out using mass assignment can can me out :)

you're doing wrong. injection, name suggests, makes sense if dependency injected from outside.

instead, have setter injection method setnotification() call within same class new notification() object argument.

there no injection going on. class still tightly coupled notification , setnotification() method serves no purpose.


Comments