将 PHP 类字段分配给数组值时将两者传递到函数中不会设置字段,为什么?
正如描述所述,我有一个函数,它接受一个数组和一个对象作为参数,并根据对象的类型将所有对象字段分配给数组中各自的值。这些对象都有不同的字段,但它们都有一个类型属性,函数使用该属性来确定要分配哪些字段。
它的工作原理如下:
function unload($arr,&$obj){ <-- //&$obj not $obj
if($obj->type == 'A'){
echo 'Setting field for A';
$obj->a = $arr['a_value'];
//some more assignments..
}
elseif($obj->type == 'B'){
$obj->b = $arr['b_value'];
echo 'Setting field for B';
//some more assignments...
}
//some more elseifs
//return an error if
//object's type doesn't
//match
else{
echo 'Error: Object type '.$obj->type.' not recognized.';
}
}
$arr['a_value'] = 'SomeValue';
$arr['b_value'] = 'SomeOtherValue';
$obj = new A(); //A's type set to 'A' upon initialization
unload($arr,$obj);
echo 'A->a set to: '.$obj->a;
输出: A->a 设置为:
代码为传入的对象输入正确的分支,但没有分配对象的任何字段。我做错了什么?
服务器正在运行 PHP 4.4.7,我仍然不知道是什么原因造成的。
编辑:我终于弄清楚了,它是两件事的组合:
我没有意识到从类内引用类字段名称时需要 $this
关键字。我假设变量具有全局作用域,因此 $this
是可选的,就像在 Java 中一样。这就是为什么仅更改函数声明并不能解决问题的原因。现在一切正常!
As the description states, I have a function that takes in an array and an object as arguments and assigns all of the objects fields to their respective values in the array depending on the type of the object. The objects all have different fields, but they all have a type attribute which the function uses to determine which fields to assign.
It works something like this:
function unload($arr,&$obj){ <-- //&$obj not $obj
if($obj->type == 'A'){
echo 'Setting field for A';
$obj->a = $arr['a_value'];
//some more assignments..
}
elseif($obj->type == 'B'){
$obj->b = $arr['b_value'];
echo 'Setting field for B';
//some more assignments...
}
//some more elseifs
//return an error if
//object's type doesn't
//match
else{
echo 'Error: Object type '.$obj->type.' not recognized.';
}
}
$arr['a_value'] = 'SomeValue';
$arr['b_value'] = 'SomeOtherValue';
$obj = new A(); //A's type set to 'A' upon initialization
unload($arr,$obj);
echo 'A->a set to: '.$obj->a;
Output:
A->a set to:
The code enters the correct branch for the object that is passed in but none of the object's fields get assigned. What am I doing wrong?
The server is running PHP 4.4.7, I still have no idea what's causing this.
Edit: I FINALLY figured it out, it was a combination of 2 things:
I didn't realize the $this
keyword was required when referencing class field names from within the class. I assumed the variables had global scope so $this
was optional like it is in Java. This is why just changing the function declaration didn't fix the problem. Now everything works fine!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您使用的是哪个 PHP 版本?
因为在 PHP4 中,您需要通过引用显式传递对象:
否则,如果您使用的是 PHP5,请仔细检查您的
$arr
内容。并在函数内部和外部执行一些print_r
...Which PHP version are you on?
Because in PHP4 you need to explicitly pass the object by reference:
If otherwise you are on PHP5, double check your
$arr
contents. And do someprint_r
inside and outside the function ...A->a
而不是$obj->a
?看来是错误的。switch
最能满足您的需求。编辑终于明白了:您必须替换
为
或以其他方式将
$obj->b = $arr['a_value'];
设置为$obj-> ;b = $arr['a'];
并对b值执行相同的操作。其含义是数组键必须相同。
get_class()
which will return the class name.A->a
instead of$obj->a
? It seems to be wrong.switch
could best suits your needs in this case.EDIT Finally got it: you have to replace
with
or otherwise set
$obj->b = $arr['a_value'];
to$obj->b = $arr['a'];
and do the same with the b value.The meaning of this is that the array keys have to be the same.