Is there a way to do these assignments more cleanly?

4

I have a class that should be started with a% of properties.

$subscription = new CheckRenew([
    'custom_2_id' => 13,
    'email'       => '[email protected]',
    'zip'         => '90530000',
    'number'      => '1234' 
]);

Inside it has a array method, which defines these resources. However, this __construct() can come without one of the elements, so before assigning I check if each element exists.

namespace App\SubscriptionHelpers;

use App\SubscriptionModels\Subscription;

class CheckRenew
{
    public $zip;
    public $email;
    public $addressNumber;
    protected $custom2Id; // service customer id (magento)

    public function __construct($info)
    {
        $this->custom2Id = null;
        if (isset($info['custom_2_id'])) {
            $this->custom2Id = $info['custom_2_id'];
        }

        $this->email = null;
        if (isset($info['email'])) {
            $this->email = $info['email'];
        }

        $this->email = null;
        if (isset($info['email'])) {
            $this->email = $info['email'];
        }

        $this->zip = null;
        if (isset($info['zip'])) {
            $this->zip = $info['zip'];          
        }

        $this->addressNumber = null;
        if (isset($info['number'])) {
            $this->addressNumber = $info['number'];         
        }
    }
}

Pretty basic. And it works, the problem is that as you can see, I've repeated a lot of code. As I am sick of DRY (do not repeat yourself), I would like, if possible, that someone would help me find a cleaner method of doing these assignments.

TL; DR

I would like to know in a cleaner, cleaner way, to check and assign my class properties.

    
asked by anonymous 29.12.2016 / 20:04

1 answer

3
For example: makes a foreach by sweeping array and passing it to the properties of your class dynamically.

<?php

class CheckRenew
{
    public $zip;
    public $email;
    public $addressNumber;
    protected $custom2Id; 
    public function __construct($info)
    {
        foreach($info as $key => $value)
        {
            $this->$key = $value;
        }
    }
}


$subscription = new CheckRenew([
    'custom2Id' => 13,
    'email'       => '[email protected]',
    'zip'         => '90530000',
    'addressNumber' => '1234' 
]);


var_dump($subscription);

Note that I changed two array names to have the same name as the items in your class.

Online Example

Addendum:

If you pass some wrong name, or pass some element that is not in the class automatically, it will be added in the scope of this class and will be accessible.

Example:

<?php

class CheckRenew
{
    public $zip;
    public $email;
    public $addressNumber;
    protected $custom2Id; 
    public function __construct($info)
    {
        foreach($info as $key => $value)
        {
            $this->$key = $value;
        }
    }
}


$subscription = new CheckRenew([
    'custom2Id' => 13,
    'email'       => '[email protected]',
    'zip'         => '90530000',
    'addressNumber' => '1234',
    'all' => true 
]);


var_dump($subscription->all);

In this example it clearly shows that an extra item has been added to your class, the array being public and accessible. You just have to be careful about this kind of event.

Example Online

    
29.12.2016 / 20:14