所以,我想通过它的属性选择一个PHP对象,就像你通过$('.element[selector=false]')
类似的方式在JS中选择一个HTML元素一样。
如何在不知道变量名称的情况下选择名为Business
的类The Night's Watch
的对象?
谢谢!
答案 0 :(得分: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;
答案 1 :(得分:0)
查找字符串The Night's Watch的查找属性名称..请执行以下代码...
$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;
}
}