1

PHP 5. 我需要将不区分大小写的 url 查询转换为 PHP 对象中的成员变量。基本上,我需要知道 url 查询键指向的成员变量,以便知道它是否为数字。

例如:

class Foo{
    $Str;
    $Num;
}

myurl.com/stuff?$var=value&num=1

处理此 URL 查询时,我需要知道“str”与 Foo->$Str 等相关联。有关如何处理此问题的任何想法?我什么都想不出来。

4

1 回答 1

1

Try something like this.

function fooHasProperty($foo, $name) {
  $name = strtolower($name);
  foreach ($foo as $prop => $val) {
    if (strtolower($prop) == $name) {
      return $prop;
    }
  }
  return FALSE;
}

$foo = new Foo;

// Loop through all of the variables passed via the URL
foreach ($_GET as $index => $value) {
  // Check if the object has a property matching the name of the variable passed in the URL
  $prop = fooHasProperty($foo, $index);
  // Object property exists already
  if ($prop !== FALSE) {
    $foo->{$prop} = $value;
  }
}

And it may help to take a look at php's documentation on Classes and Objects.

Example:

URL: myurl.com/stuff?var=value&num=1

Then $_GET looks like this:

array('var' => 'value', 'num' => '1')

Looping through that, we would be checking if $foo has a property var, ($foo->var) and if it has a property num ($foo->num).

于 2013-03-17T03:57:12.347 回答