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.
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
@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.
– novic