mySql PHP - return object value with space in key

江枫思渺然 提交于 2019-12-03 04:38:12

You use the following syntax

 $ob->{'Device Vendor'}

The syntax is this:

$ob->{'Device Vendor'}

I'm having a hard time trying to find an explicit reference to this in the PHP manual. I'm afraid that it needs to be inferred and you can only do so if you already know the answer. At Classes and Objects-> Properties they say:

Class member variables are called "properties". [...] They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration.

The rules that applies now is Variable variables:

In order to use variable variables with arrays, you have to resolve an ambiguity problem. That is, if you write $$a1 then the parser needs to know if you meant to use $a1 as a variable, or if you wanted $$a as the variable and then the 1 index from that variable. The syntax for resolving this ambiguity is: ${$a1} for the first case and ${$a}1 for the second.

We are basically abusing variable variables so we can use a space.

Karan

Replace the spaces with underscores in the property name with the property name in all lower case:

if $property = 'Device Vendor';

    use $property = 'device_vendor';
    $ob->{$property};
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!