Home > other >  Undefined variable $jobsDTO
Undefined variable $jobsDTO

Time:08-27

I works with API and I try to use DTO:

Jobs/PayrollReportJob

class PayrollReportJob implements ShouldQueue
{

    public function __construct($data, $jobWatcherId)
    {
        $this->jobsDTO = new JobsDTO($data['data'],
                               $data['working_days'], $data['holiday_hours'],
                               $data['advance_payroll_date'], $data['main_payroll_date']);
    }

    public function handle()
    {     
        try {

            $redmineService = new RedmineAPIService();
            foreach ($jobsDTO->getUser() as $user) {
            
            }

JobsDTO

class JobsDTO
{
    public function getUser(): array
    {
        return $this->users;
    }

I have mistale for line: foreach ($jobsDTO->getUser() as $user) {. How can I resolve it?

CodePudding user response:

You trying to access variable in handle() method scope, which is not exists, i believe.

So, you should declare field jobsDTO for PayrollReportJob and call its getUser method, for example:

class PayrollReportJob implements ShouldQueue
{
    private JobsDTO $jobsDTO;

    public function __construct($data, $jobWatcherId)
    {
        $this->jobsDTO = new JobsDTO($data['data'],
                               $data['working_days'], $data['holiday_hours'],
                               $data['advance_payroll_date'], $data['main_payroll_date']);
    }

    public function handle()
    {     
        try {

            $redmineService = new RedmineAPIService();
            foreach ($this->jobsDTO->getUser() as $user) {

            }
  • Related