2016-05-16 108 views
1

所以,我想通过它的属性来选择一个PHP对象,就像你如何通过$('.element[selector=false]')有点东西在JS中选择一个HTML元素。选择一个类使用属性作为选择器 - PHP

我该怎么去选择类别Business的对象名称为The Night's Watch,而不知道它的变量名?

谢谢!

+1

您需要更具体的:这个“商业”对象存储在哪里?我想乍一看你的应用程序设计不合理:你应该能够选择对象并根据你选择的标准存储它们。 – Arcesilas

+1

这取决于你的对象存储在哪里。你想从哪里搜索这个? – DaGardner

+0

粘贴代码,然后我们可以帮助你。 – prava

回答

2

也不是那么困难,因为它似乎

// this will be array of all variables 
$vars = get_defined_vars(); 
foreach(array_keys($vars) as $v) 
    if (gettype($$v) == 'object' and   
     get_class($$v) == 'Business' and  // get_class returns class name 
     $$v->name == 'The Night's Watch')  // test property 
     echo 'Variable you find is ' . $v; 
+0

有一些数据存储在一个可变的开发人员不知道... – Arcesilas

+0

我不知道,也许他写了一个调试器 – splash58

0

为了找到字符串守夜..请做下面的代码的属性名称...

$string_to_find = "The Night's Watch"; 
//suppose $obj is the object of the class Business 
foreach ($obj as $property => $value) { 
     if ($value == $string_to_find) { 
      // You found the string 
      echo $property; 
      echo $value; 
     } 
    }