Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

php: Select object property with a dynamic name ($obj->dynamicNameHere)?

Posted on 2012-03-29
4
Medium Priority
?
436 Views
Last Modified: 2012-03-31
I should know this, but here I am. I'm trying to select an object property with a non-static name like this:

<?php
$properties = array('property1','property2','property3');
foreach($properties as $property) {
  print $theObject->[[[$property]]]
}
?>

Open in new window


Any thoughts on how to do that? I know it's probably super simple.

Thanks!!!

Chris
0
Comment
Question by:Ultrus
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 10

Expert Comment

by:acbxyz
ID: 37784633
Hello Chris,

since properties are called without a dollar sign behind the arrow, you can simply use a dollar for a dynamic name. Here an example working for me:
// Defining test class
class test{
    public $variable;
}   
// Creating an instance of it
$testobj = new test();
// preset a value
$testobj->variable = 'Wert 1';
// name the variable, can be a loop or something
$varname = 'variable';
// assigning a new value to the named variable above
$testobj->$varname = 'Wert 2';
// let's see if it works
echo $testobj->variable;

Open in new window

0
 
LVL 10

Author Comment

by:Ultrus
ID: 37785033
acbxyz,
I figured it out. If you respond with "Yes, that looks great!", I'll give your response all the points. ;)

<?php
$properties = array('property1','property2','property3');
foreach($properties as $property) {
  print $theObject->{$property}
}
?>

Open in new window

0
 
LVL 10

Accepted Solution

by:
acbxyz earned 2000 total points
ID: 37789279
Yes, that looks great! ;-)

If $property is a simple string variable as in this example, you even don't need the curly braces, but you can.
0
 
LVL 10

Author Closing Comment

by:Ultrus
ID: 37792189
Thanks! :D
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…
This video teaches users how to migrate an existing Wordpress website to a new domain.
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question