PHP - 通过变量获取对象属性
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13440244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP - Get object properties by variable
提问by Hyman
I have an object with lots of properties. Some of the properties have their names start with the same string of text (in my example to come "bullet"), followed by an integer.
我有一个有很多属性的对象。一些属性的名称以相同的文本字符串开头(在我的示例中为“bullet”),后跟一个整数。
I can fetch the property values as follows:
我可以按如下方式获取属性值:
echo $objectName->bullet1;
echo $objectName->bullet2;
echo $objectName->bullet3;
and so on.
等等。
I'm trying to write a for loop to get the first 20 of these, and at the moment it looks a bit like:
我正在尝试编写一个 for 循环来获取其中的前 20 个,目前它看起来有点像:
for ($i = 1; $i <= 20; $i++){
if ($objectName->bullet$i){
echo $objectName->bullet$i;
}
}
But this isn't working. I know I could write something like
但这不起作用。我知道我可以写一些类似的东西
$bulletsArray[1] = $objectName->bullet1;
$bulletsArray[2] = $objectName->bullet2;
$bulletsArray[3] = $objectName->bullet3;
all the way through to 20, then put a for loop on that, but I'm sure there must be a cleaner way. Can someone point me in the right direction?
一直到 20,然后在上面放一个 for 循环,但我相信一定有更干净的方法。有人可以指出我正确的方向吗?
回答by Botond Balázs
This is how you can do it:
你可以这样做:
for ($i = 1; $i <= 20; $i++){
$propertyName = "bullet$i";
if ($objectName->$propertyName){
echo $info->$propertyName;
}
}
Though I think using an array instead of the object would be a better solution.
虽然我认为使用数组而不是对象会是一个更好的解决方案。
回答by Sébastien Renauld
Both previous answers have the "try it, if it doesn't work, move on" and are working for a very specific situation. Let me give you a more generic approach, which, sadly, requires PHP 5.
之前的两个答案都有“尝试一下,如果它不起作用,请继续”并且正在针对非常特定的情况。让我给你一个更通用的方法,遗憾的是,它需要 PHP 5。
A bit about Reflection
关于反射的一点
Reflection allows you to reflect on an object or class to extract useful information about it. In particular, you can use Reflection to get all the properties set in a class, whether static, at run-time, or generated on the go.
反射允许您对对象或类进行反射以提取有关它的有用信息。特别是,您可以使用反射来获取类中设置的所有属性,无论是静态的、运行时的还是在运行中生成的。
To do this, start by initiating your Reflection object (assuming your class object is $object) as follows:
为此,首先启动您的 Reflection 对象(假设您的类对象是 $object),如下所示:
$refObj = new ReflectionObject($object);
This will now give you a reflection interface. Notice the useful ReflectionObject::getProperties()method - it allows you to get all properties set in a class (and you can filter by public, protected, private etc... if needed). We'll use exactly this:
现在,这将为您提供一个反射界面。注意有用的ReflectionObject::getProperties()方法 - 它允许您获取类中设置的所有属性(如果需要,您可以按公共、受保护、私有等进行过滤)。我们将使用这个:
$yourNewArray = array();
$PropertyArray = $refObj->getProperties();
$pCount = count($PropertyArray);
for ($i = 0; $i < $pCount; $i++) {
$yourNewArray[$PropertyArray[$i]->getName()] = $PropertyArray[$i]->getValue($object);
}
Done. Completely generic, works anywhere.
完毕。完全通用,适用于任何地方。
回答by Lucius
$var = 'myVariable';
$value = $object->$var;
is the correct syntax for accessing a field by name in PHP.
是在 PHP 中按名称访问字段的正确语法。
In your case it would look something like this:
在您的情况下,它看起来像这样:
for ($i = 1; $i <= 20; $i++)
{
$var = 'bullet'.$i;
if ($objectName->$var)
{
echo $info->$var;
}
}
回答by el jonco
in my system, which doesn't allow me to do
在我的系统中,这不允许我这样做
$myPropertyValue = $object->$property;
I still can get to the value of the 'variable' property name with the function below; you can set $property without php complaining about syntax or throwing errors.
我仍然可以使用下面的函数获取“变量”属性名称的值;你可以设置 $property 而不会 php 抱怨语法或抛出错误。
<?php
class MyClass{
public function getProperty($propertyName){
$props=get_object_vars($this);
if (array_key_exists($propertyName,$props)){
return $props[$propertyName];
}else{
return FALSE;
}
}
}
?>

