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

Posted on 2012-03-29
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:

$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.


Question by:Ultrus
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
LVL 10

Expert Comment

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

LVL 10

Author Comment

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

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

Open in new window

LVL 10

Accepted Solution

acbxyz earned 500 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.
LVL 10

Author Closing Comment

ID: 37792189
Thanks! :D

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Suggested Solutions

There’s a good reason for why it’s called a homepage – it closely resembles that of a physical house and the only real difference is that it’s online. Your website’s homepage is where people come to visit you. It’s the family room of your website wh…
In threads here at EE, each comment has a unique Identifier (ID). It is easy to get the full path for an ID via the right-click context menu. However, we often want to post a short link within a thread rather than the full link. This article shows a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

726 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