Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - Get object properties by variable

Tags:

syntax

object

php

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.

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 ($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?

like image 547
Jack Avatar asked Nov 18 '12 12:11

Jack


People also ask

How do I get the properties of an object in PHP?

The get_object_vars() function is an inbuilt function in PHP that is used to get the properties of the given object. When an object is made, it has some properties. An associative array of properties of the mentioned object is returned by the function. But if there is no property of the object, then it returns NULL.

What is $this in PHP with example?

$this is a reserved keyword in PHP that refers to the calling object. It is usually the object to which the method belongs, but possibly another object if the method is called statically from the context of a secondary object. This keyword is only applicable to internal methods.

What does :: class do in PHP?

SomeClass::class will return the fully qualified name of SomeClass including the namespace. This feature was implemented in PHP 5.5. It's very useful for 2 reasons. You can use the use keyword to resolve your class and you don't need to write the full class name.

How do you access properties and methods Explain with example and also explain $this variable in PHP?

For example, you can access the $name property by using $this->name (note that you don't use a $ before the name of the property). An object's methods can be accessed in the same way; for example, from inside one of person's methods, you could call getName() by writing $this->getName().


2 Answers

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.

like image 75
Botond Balázs Avatar answered Oct 02 '22 09:10

Botond Balázs


$var = 'myVariable';
$value = $object->$var;

is the correct syntax for accessing a field by name in 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;
     }
}
like image 22
Lucius Avatar answered Oct 02 '22 07:10

Lucius