laravel-actions icon indicating copy to clipboard operation
laravel-actions copied to clipboard

Constructor variables overriden

Open makopov opened this issue 2 years ago • 8 comments

I observed some odd behavior when trying to store a passed in variable.

My constructor in the base class assigns a variable, but when the child class goes to evaluate it, its only got an empty object. Here's an example:

Base Class:

public function __construct(User $user) {
  $this->user = $user;
}

Then the child class:

public function handle() {
  dd($this->user);
}

I call this like so

(new class_name($user))->run();

but I've also done

class_name::run($user);

And the result is the same.

This will print an empty user object with no attributes. So its doing the assignment. If I do the dd in the constructor it will have all the data, but only when its called from other functions does it come out empty.

I've tried many variations of this. But ultimately when taking out the trait does this work normal.

makopov avatar Jan 06 '23 22:01 makopov