[英]PHP how to assign class property variables conditionally
I'm new to php classes, arrays, etc, so pls excuse me if I don't use the correct terminology. 我是php类,数组等的新手,所以如果我不使用正确的术语,请原谅。 What I'm looking for is how to assign easily values to properties from a class without having to depend on "if" statements.
我正在寻找的是如何轻松地为类中的属性赋值,而不必依赖于“if”语句。
For example: 例如:
Suppose I have an instance of a class test, and "test" has a property "employee" (hope this is the correct way to call it) and employee is of a complex type. 假设我有一个类测试的实例,“test”有一个属性“employee”(希望这是调用它的正确方法),而employee是一个复杂的类型。
So, I have something like: 所以,我有类似的东西:
$test -> employee = array('Age' => '30', 'Sex' =>$sex, 'nationality'=>$nationality, 'maritalstatus'=>$status, etc, etc)
The problem I have here is, what if 'Age", 'Sex', 'Nationality', etc are not always present and I only want to assign values to them when they have something assigned , and I don't want to use If's for each combination of not empty values ...(this is a short example, but I have a lot of these attributes or whatever they are called and too many "if" combinations is too messy)... 我在这里遇到的问题是,如果“年龄”,“性别”,“国籍”等并不总是存在,我只想在分配某些东西时为它们分配值,我不想使用If的对于每个非空值组合......(这是一个简短的例子,但我有很多这些属性或者它们被称为什么,太多“如果”组合太乱了)......
I'm sending these values later as a soap request and I don't want any empty xml tags... 我稍后将这些值作为soap请求发送,我不想要任何空的xml标签......
My apologies if my terminology is not correct, but I hope I've been clear enough for someone out there to help me out! 如果我的术语不正确,我很抱歉,但我希望我已经足够清楚了解那里有人来帮助我!
Thanks in advance, Pablo 提前谢谢,巴勃罗
What you could do in this case: 在这种情况下你可以做什么:
setEmployee
that requires all arguments to be provided. setEmployee
。 Here is some example code; 这是一些示例代码; try this out:
试试这个:
<?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());
?>
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;
Have you considered a short ternary solution? 你考虑过一个简短的三元解决方案吗?
'Sex' => ( isset($sex) ? $sex : "n/a" )
This is essentially performing if-else
logic, but not nearly as verbose. 这基本上是执行
if-else
逻辑,但不是那么冗长。 Our condition is isset($sex)
. 我们的条件是
isset($sex)
。 If this is true, we return $sex
, else we return "n/a"
. 如果这是真的,我们返回
$sex
,否则我们返回"n/a"
。 Whatever is returned becomes the value of 'Sex'
. 无论返回什么,都会成为
'Sex'
的价值。
If this isn't sufficient, I would encourage you to require valid values during instantiation. 如果这还不够,我建议您在实例化期间要求有效值。 If the user doesn't provide proper and expected values, refuse instantiation of the class.
如果用户未提供正确和期望的值,请拒绝实例化该类。
$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: 如果你需要接受
FALSE
作为值,那么请改用:
$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 makes sure only keys with non-null or non-falsey values are present in the final array. 编辑:这可确保最终数组中只存在具有非null或非falsey值的键。
One way would be to store your possible keys in an array - then you could iterate over it only need one set of control code for each array: 一种方法是将可能的密钥存储在一个数组中 - 然后你可以迭代它,每个数组只需要一组控制代码:
$keys = array('Age', 'Sex', 'Nationality');
foreach ($keys as $key) {
if ( isset($inputs[$key]) )
$test->employee[$key] = $inputs[$key];
}
I am a tad confused on your problem. 我对你的问题感到困惑。 Are the variables such as $sex, $nationality, and so forth not always defined or are they just sometimes set to null?
诸如$ sex,$ nationality等变量是不是总是被定义,或者它们有时被设置为null? If those variables do not always exist, it is imperative to check for their existence first and there is no way around that - unless you refactor earlier code.
如果这些变量并不总是存在,则必须首先检查它们的存在,并且没有办法解决这个问题 - 除非你重构早期的代码。
I am presuming the variables do exist and you just do not want to save NULL cases. 我假设变量确实存在,你只是不想保存NULL情况。
An OOP approach may be to use data encapsulation - a common choice for a case like this. OOP方法可能是使用数据封装 - 这种情况的常见选择。 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 employee as its own object, and use mechanisms in its object to skip null values. 另一种方法是将employee作为自己的对象引入,并在其对象中使用机制来跳过null值。 You might override __set, use data encapsulation again, take __construct arguments, or a variety of other solutions.
您可以覆盖__set,再次使用数据封装,获取__construct参数或其他各种解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.