将 PHP 类字段分配给数组值时将两者传递到函数中不会设置字段,为什么?

发布于 2024-10-04 08:56:33 字数 1096 浏览 4 评论 0原文

正如描述所述,我有一个函数,它接受一个数组和一个对象作为参数,并根据对象的类型将所有对象字段分配给数组中各自的值。这些对象都有不同的字段,但它们都有一个类型属性,函数使用该属性来确定要分配哪些字段。

它的工作原理如下:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

生活了然无味 2024-10-11 08:56:33

您使用的是哪个 PHP 版本?

因为在 PHP4 中,您需要通过引用显式传递对象:

function unload($arr,&$obj){

否则,如果您使用的是 PHP5,请仔细检查您的 $arr 内容。并在函数内部和外部执行一些 print_r ...

Which PHP version are you on?

Because in PHP4 you need to explicitly pass the object by reference:

function unload($arr,&$obj){

If otherwise you are on PHP5, double check your $arr contents. And do some print_r inside and outside the function ...

相思故 2024-10-11 08:56:33
  • 如果您想获取类名,我建议您使用 get_class() 它将返回类名。
  • 无论如何,为什么你使用 A->a 而不是 $obj->a ?看来是错误的。
  • 请注意,在这种情况下,switch 最能满足您的需求。

编辑终于明白了:您必须替换

$arr['a'] = 'SomeValue';
$arr['b'] = 'SomeOtherValue';

$arr['a_value'] = 'SomeValue';
$arr['b_value'] = 'SomeOtherValue';

或以其他方式将 $obj->b = $arr['a_value']; 设置为 $obj-> ;b = $arr['a'];并对b值执行相同的操作。
其含义是数组键必须相同。

  • If you want to get the class name i'd suggest you to use get_class() which will return the class name.
  • Anyway why are you using A->a instead of $obj->a? It seems to be wrong.
  • And notice that switch could best suits your needs in this case.

EDIT Finally got it: you have to replace

$arr['a'] = 'SomeValue';
$arr['b'] = 'SomeOtherValue';

with

$arr['a_value'] = 'SomeValue';
$arr['b_value'] = 'SomeOtherValue';

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文