lorisleiva / laravel-actions

⚡️ Laravel components that take care of one specific task
https://laravelactions.com
MIT License
2.41k stars 117 forks source link

Constructor variables overriden #218

Open makopov opened 1 year ago

makopov commented 1 year ago

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.

Wulfheart commented 1 year ago

Some ideas:

  1. Is $this->user defined? (Unlikely, just to be sure)
  2. Which language versions do you use?
  3. Could you please try this again by making it a plain PHP class? Does the issue still persist?
makopov commented 1 year ago

Hey there thanks for the reply. I'll answer these inline..

  1. It is defined, however its empty. It resembles as if someone just did a new User(), no attributes defiend, just an empty object. This is the weirdest part about it.
  2. PHP 8.1, Laravel 9
  3. I did, the issue goes away. In fact for now my work around is to remove the trait and define my own static run function. I'd like to not keep this solution forever but it solves my immediate need.
Wulfheart commented 1 year ago

Hm. Weird. Which trait do you use exactly?

makopov commented 1 year ago

Just the AsAction

Wulfheart commented 1 year ago

Have you tried debugging it with xDebug? Do you have a repo (prefered with a Dockerfile) where I can try to reproduce it?

makopov commented 1 year ago

I do not. I can try to set something up.

sneycampos commented 1 year ago

Could you show more code? like the parent's construct initialization?

makopov commented 1 year ago

Could you show more code? like the parent's construct initialization?

I haven't had time to look at this yet, but its on my list to get to soon as my temporary solution will hit its ceiling.