Is there a way to do these assignments more cleanly?

Asked

Viewed 38 times

4

I have a class that should be started with a array property.

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

Inside it has a way __construct(), defining these resources. However, this array can come without one of the elements, so before assigning I check if there is each element.

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'];         
        }
    }
}

Very basic. And it works, the problem is that as you can see, I repeated a lot of code. As I am sick by DRY (don’t repeat yourself), I would like, if possible, someone to help me find a cleaner method of doing these assignments.

TL;DR

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

1 answer

3


For example: makes a foreach sweeping the array and passing it on to your class properties in a dynamic manner.

Observing: the keys to the array has to have the same name as the itens class

<?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 your class items.

Online Example


Addendum:

If you pass a wrong name, or else pass an element that is not in the class automatically it will be added in the scope of that 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 clearly shows that an item has been added to its class the all being public and accessible. Just take care of this type of event.

Online Example

  • 1

    Wow, I like your approach! I’ll just wait a little longer to see if someone comes up with a solution as good as this

  • @wdarking, ok !!! can improve more, you can check the class items, but, this mode is as simple as possible. The good this way is a pattern of names that can facilitate in its development.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.