Getting the null value returned when using the attribute, but var_dump in the class returns the correct data

Alright, stay with me here. I have a process that I need to execute, and there are quite a few if statements that I break down into smaller classes, instead of having one ugly switch or if / else statement. Basically, I call the class descriptor method, it calculates where we are in the process, creates the corresponding class, which then collects the necessary collection and returns it.

I have a model in which I am trying to get nextAction for this particular record. nextAction will have a name, date, Remaining time, and route keys returned as a collection. Here is my FreeLook.php method:

 public function nextAction() { // handle basically just calls a handle method on the NextActionHandler class. return handle(new NextActionHandler($this)); } 

The NextActionHandler class defines where we are in the process. He does this using a property that I created with the name Actionable . Here is the NextActionHandler class:

 class NextActionHandler { use Actionable; protected $freeLook; public function __construct($freeLook) { $this->freeLook = $freeLook; } public function handle() { switch (true) { case $this->wantsSign() && !$this->signJobIsComplete(): return handle(new SignNextAction($this->freeLook)); case $this->wantsPaint() && !$this->paintJobIsComplete(): return handle(new PaintNextAction($this->freeLook)); case $this->needsToSubmitCoOp(): return handle(new CoOpNextAction($this->freeLook)); default: return handle(new DefaultNextAction($this->freeLook)); } } } 

Here are some of the signs of Actionable . I do not put all this here because it is really long. I contain a lot of logic to figure out where we are in the process. It should be noted that it contains logic for constructing the output of the collection that I am returning. (if you want the full Actionable code, go here )

  protected function needToChooseVendor() { $this->name = 'Choose Vendor'; $this->duration = $this->freeLook->status->expected_duration; $this->calculateTimeRemaining(); return $this->buildOutput(); } protected function calculateTimeRemaining() { if ($this->duration) { $this->timeRemaining = Carbon::now()->diffInDays($this->date); } } protected function buildOutput() { return collect([ 'name' => $this->name, 'date' => $this->date, 'timeRemaining' => $this->timeRemaining, 'route' => $this->route, ]); } 

And finally, my class is DefaultNextAction , which also uses the attribute to build the output and return it.

 class DefaultNextAction { use Actionable; public function __construct($freeLook) { $this->freeLook = $freeLook; $this->date = $freeLook->next_action_at; } public function handle() { return $this->returnDefaultNextAction(); } protected function returnDefaultNextAction() { $this->name = $this->freeLook->status->name; $this->duration = $this->freeLook->status->expected_duration; $this->calculateTimeRemaining(); $this->output = $this->buildOutput(); return $this->output; } } 

So, when I dump and die in the returnDefaultNextAction method, I get the expected results. But, when I reset and die in my model, I always get null . What would be the reason for this? Since I use a trait and traits cannot be created, I thought I was dealing with the same instance of the object, and everything should work.

Any ideas would be very helpful!

EDIT 1 Here is the handle method that I use to wrap all of my objects.

 if (!function_exists('handle')) { function handle($object) { if (!is_object($object)) { throw new Exception('An object must be passed to the handle method. This is not what happened.'); } call_user_func([$object, 'handle']); } } 

EDIT 2 Even if I name my descriptor method in my model, I get the same null result:

  public function nextAction() { $nextAction = new NextActionHandler($this); return $nextAction->handle(); } 
+5
source share
1 answer

Your handle function returns nothing. Must be

 if (!function_exists('handle')) { function handle($object) { if (!is_object($object)) { throw new Exception('An object must be passed to the handle method. This is not what happened.'); } return call_user_func([$object, 'handle']); } } 
+2
source

Source: https://habr.com/ru/post/1261720/


All Articles