不能静态调用非静态方法



我对如何实现这一点感到非常困惑。我所想做的就是能够从另一个类调用函数并返回值。

带电组件

use LivewireComponent;
use AppActionsBroadcastGetCurrentActiveTimeSlotAction;
class DisplayLiveBroadcastCard extends Component
{
public $timeSlot;
public function mount()
{
$this->refreshTest();
dd($this->timeSlot);
}
public function refreshTest()
{
$this->timeSlot = GetCurrentActiveTimeSlotAction::execute();
}

在GetCurrentActiveTimeslot类内

class GetCurrentActiveTimeSlotAction
{
public $test;
public function __construct()
{
$this->test = 5;
}
public function execute()
{
$value = $this->test;
return $value;
}
}

是的,我确实将其重命名为static function execute(),但这打破了另一个问题,现在我在尝试时出错

static function execute()
{
$value = $this->test;
return $value;
}

或者,我也尝试过这种方法,但现在它说我需要将一个变量传递到refreshTest函数中。我理解,但我在那里经过的任何东西似乎都会破坏它。


public function mount()
{
$this->refreshTest();
dd($this->timeSlot);
}
public function refreshTest(GetCurrentActiveTimeSlotAction $getCurrentActiveTimeSlotAction)
{
$this->timeSlot = $getCurrentActiveTimeSlotAction->execute();
}

正在寻找关于如何在GetCurrentActiveTimeSlotAction中进行计算并返回livewire组件内部的值的任何建议。

假设您不想做琐碎的事情(例如$this->timeSlot = (new GetCurrentActiveTimeSlotAction)->execute();(,而是想进行依赖项注入(因为这将使您的代码更易于测试(,那么您可以在mount方法(源代码(中注入对象:

use LivewireComponent;
use AppActionsBroadcastGetCurrentActiveTimeSlotAction;
class DisplayLiveBroadcastCard extends Component
{
public $timeSlot;
private $activeTimeslotActionGetter;
public function mount(GetCurrentActiveTimeSlotAction $getter)
{
$this->activeTimeslotActionGetter = $getter;
$this->refreshTest();
dd($this->timeSlot);
}
public function refreshTest()
{
$this->timeSlot = $this->activeTimeslotActionGetter->execute();
}

最新更新