Initiating Private Property via loop and checking exists before accessing it

Viewed 24

I'm using Laravel Job to process some data, passing a device object which has json array in it.

defined private properties, Note there are way more than just 3

private int $width;
private int $depth;
private int $height;

Then initiating it via loop

foreach($this->device->data as $key => $val)
{
        $this->{$key} = $val;
}

Now before using it I want to check if they initiated properly before running my methods.

Currently checking before running each method like

if(ISSET($this->waste) && ISSET($this->height))
            $this->level = $this->getLevelFromDistance($this->waste, $this->height);

Is it the only way to go about this? or is there a better way to do this?

My ultimate object is to check if properties exists then process the object otherwise log an alert for user.

0 Answers
Related