【发布时间】:2017-04-27 22:27:38
【问题描述】:
我试图从我的刀片视图中调用App\Http\Timeslot::isOpen(),但我不明白如何在不将 isOpen() 声明为静态的情况下调用它。我不能将其声明为静态,因为我需要使用构造中的 $this->hours。如果我不声明它,静态 laravel 将返回 Cannot redclare Class error
有人可以建议我应该如何编写这个以便我仍然可以访问 $this->hours 变量吗?
刀片模板:
@if(App\Http\Timeslot::isOpen())
We're open
@else
We're closed
@endif
时隙类
<?php namespace App\Http;
use App\OpeningHour;
use Carbon\Carbon;
class Timeslot
{
protected $hours;
public function __construct()
{
$this->hours = OpeningHour::all();
}
public static function isOpen()
{
// get current date
Carbon::now()->format('w');
$open_window = $this->hours->get(Carbon::now()->format('w'));
// is it over current days' opening hours?
if(Carbon::now()->toTimeString() > $open_window->opening_time)
return true;
else
return false;
}
}
【问题讨论】:
标签: php laravel oop ioc-container