Best Practice - Check if property exist and assign
Tags: php, programming, style, syntax
Published: 2018-08-07 17:56:51

---
title: Best Practice - Check if property exist and assign 
published: true
description: Better way's of checking if properties exist and assing
tags: php, programming, style, syntax
---

Hi all,

from time to time you might need to assign properties or similar to variables and in some cases some don't exist.

In PHP and most other languages it's pretty straightforward to check for example:

~~~~~~~~ php
if(isset($issue['samples']))
    $i->samples = $issue['samples'];
~~~~~~~~

This is also ok for multiple cases:

~~~~~~~~ php
if(isset($issue['samples']))
    $i->samples = $issue['samples'];

if(isset($issue['summary']))
    $i->summary = $issue['summary'];

if(isset($issue['property_names']))
    $i->property_names = $issue['property_names'];

if(isset($issue['description']))
    $i->description = $issue['description'];

if(isset($issue['count']))
    $i->count = $issue['count'];
~~~~~~~~

And at some point I came up with inline if's which makes it easier to write/read and maintain.
	
~~~~~~~~ php
$i->samples = (isset($issue['samples']) ? $issue['samples'] : null);
$i->summary = (isset($issue['summary']) ? $issue['summary'] : null);
$i->property_names = (isset($issue['property_names']) ? $issue['property_names'] : null);
$i->description = (isset($issue['description']) ? $issue['description'] : null);
$i->count = (isset($issue['count']) ? $issue['count'] : null);
~~~~~~~~

But I feel there might be other and better way's of handling this.


Here the full example with a foreach-loop:

~~~~~~~~ php
foreach($data['quality_issues']['data'] as $issue){
    if($issue['property_names'] != 'product_category'){
        print_r($issue);
        $i = new QualityIssue();
        $i->samples = (isset($issue['samples']) ? $issue['samples'] : null);
        $i->summary = (isset($issue['summary']) ? $issue['summary'] : null);
        $i->property_names = (isset($issue['property_names']) ? $issue['property_names'] : null);
        $i->description = (isset($issue['description']) ? $issue['description'] : null);
        $i->count = (isset($issue['count']) ? $issue['count'] : null);
        $i->error = (isset($issue['error']) ? $issue['error'] : null);
        $i->name = (isset($issue['name']) ? $issue['name'] : null);
        $i->type = (isset($issue['type']) ? $issue['type'] : null);
        $i->level = (isset($issue['level']) ? $issue['level'] : null);
        $i->category = (isset($issue['category']) ? $issue['category'] : null);

        array_push($partner->catalog->quality_issues, $i);
    }
}
~~~~~~~~

Maybe I'm overthinking this and the inline if is already the "perfect" solution (as if this exists :)) but I would like to hear from you how you normally handle this and if you came up with good practices that might help to improve this.

Cheers,
Michael


CONCLUSION:

Really appreciate the great feedback in the comments and wanted to describe the solution which I found most suitable suggested by @Franco Traversaro:

~~~~~~~~ php
class QualityIssue{
    public static function fromArray(array $source) : self
    {
        $i = new self(); 
        foreach ($source as $property => $value) {
            $i->{$property} = $value;
        }
        
        return $i;
    }
}


foreach($data['quality_issues']['data'] as $issue){
    if($issue['property_names'] != 'product_category'){
        array_push($partner->catalog->quality_issues, QualityIssue::fromArray($issue));
    }
}
~~~~~~~~