PHP how to conditionally assign class property variables

I am new to php classes, arrays, etc., so forgive me if I do not use the correct terminology. I am looking for how to easily assign values ​​to properties from a class, not depend on if statements.

For instance:

Suppose I have an instance of a class test, and "test" has the "employee" property (I hope this is the correct way to call it), and the employee has a complex type.

So, I have something like:

$test -> employee = array('Age' => '30', 'Sex' =>$sex, 'nationality'=>$nationality, 'maritalstatus'=>$status, etc, etc) 

The problem that I have here is that if "Age", "Sex", "Citizenship", etc. are not always present, and I only want to assign values ​​to them when they have something assigned , and I do not want to use If for each combination of non-empty values ​​... (this is a brief example, but I have a lot of these attributes, or whatever they were not called, and too many β€œif” combinations are too dirty) ...

I send these values ​​later as a soap request and I don't need empty xml tags ...

My apologies if my terminology is incorrect, but I hope that I was clear enough for someone there to help me!

Thanks in advance, Pablo

+4
source share
6 answers

How about this:

 $array = array('Age' => '30' , 'Sex' => $sex, 'nationality' => $nationality, 'maritalstatus' => $status); foreach ($array as $key => $value) { if (is_null($value) || $value=="") { unset($array[$key]); } } $test->employee = $array; 
+1
source

What could you do in this case:

  • Make employee a private attribute
  • Define the "setter" setEmployee , which requires all arguments to be provided.

Here is a sample code; try it:

  <?php class test{ private $employee; public function setEmployee($age,$sex,$nationality,$maritalstatus){ $this->employee=array('Age'=>$age, 'Sex'=>$sex, 'Nationality'=>$nationality, 'MaritalStatus'=>$maritalstatus); } public function getEmployee(){ return $this->employee; } } $t=new test(); $t->setEmployee('32','M','American','Married'); print_r($t->getEmployee()); ?> 
+3
source

Have you considered a short triple solution?

 'Sex' => ( isset($sex) ? $sex : "n/a" ) 

This is essentially the execution of if-else logic, but not so much. Our condition is isset($sex) . If this is true, we return $sex , otherwise return "n/a" . Everything that returns becomes the value of 'Sex' .

If this is not enough, I would recommend that you require valid values ​​during instance creation. If the user does not provide the correct and expected values, discard the class instance.

+1
source
 $temp_array = array('Age' => '30', 'Sex' =>$sex, 'nationality'=>$nationality, 'maritalstatus'=>$status, etc, etc); foreach($temp_array as $key => $val){ if(!$temp_array[$key]){ unset($temp_array[$key]; } } 

if you need to accept FALSE as a value, then use this instead:

 $temp_array = array('Age' => '30', 'Sex' =>$sex, 'nationality'=>$nationality, 'maritalstatus'=>$status, etc, etc); foreach($temp_array as $key => $val){ if($temp_array[$key] !== NULL){ unset($temp_array[$key]; } } 

edit: this ensures that only keys with non-zero or non-false values ​​are present in the final array.

0
source

One way is to store your possible keys in an array - then you can iterate over it only one set of control codes for each array:

 $keys = array('Age', 'Sex', 'Nationality'); foreach ($keys as $key) { if ( isset($inputs[$key]) ) $test->employee[$key] = $inputs[$key]; } 
0
source

I am a little confused about your problem. Are variables like $ sex, $ nationality etc. are not always defined or are they sometimes set to null? If these variables do not always exist, you must first check their existence, and there is no way around this - unless you reorganize the earlier code.

I assume the variables exist and you just don't want to save NULL cases.

An OOP approach may be to use data encapsulation - a common choice for this case. These following changes should be made to the test class.

 class test { protected $employee = array(); public function getEmployee() { return $this->employee; } public function setEmployee($employee) { foreach ($employee as $key => $value) { if ($value !== null) { // or any value it should not be $this->employee[$key] = $value; } } } ... 

Another approach would be to introduce the employee as his own object and use the mechanisms in his object to skip zero values. You can override __set, reuse data encapsulation, accept __construct arguments, or many other solutions.

0
source

Source: https://habr.com/ru/post/1300093/


All Articles