Jump to content

Retrieving Properties Dynamically


LostInTheWoods

Recommended Posts

Hi,

 

I need a little help with retrieving properties from an object, but having the name of the property passed in to a function. Er code might help explain...

 

Here 'version'is the name of the object property I am trying to retrieve:

 

// Property name hard coded in function
function myFunction1($object){

  print($object->version);

}

// Property passed in as a parameter.
function myFunction2($object, $propertyName){

  print($object->$propertyName);     // Wrong!
  
  print($object->[$propertyName]);   // Wrong!
  
  print($object->[propertyName]);    // Wrong!

}

 

So how can I retrieve an object's property by it's name, but dynamically?

 

Many Thanks,

Stephen

Link to comment
Share on other sites

Hi, I might be mistaken, but I believe you can do exactly what you want, and you was correct on how to do it, not sure why it didn't work! I have given the code below for what I believe you are trying to do:

 

class test
{
public $version;
public function __construct()
{
	$this->version = 5;
}
}

function getproperty(test $object, $property)
{
echo $object->$property;
}

$foo = new test();
getproperty($foo, 'version');

 

One thing you might want to check on your code is the scope of the property you are trying to access (it must be public and defined as such).

 

 

Link to comment
Share on other sites

Hi,

 

This is weird, I have tried your example code and it works fine. The complete scenario of what I am trying to do is of course more complicated than I gave in my first example, but I can't see why this weird behaviour is happening. Here is the bigger picture:

 

I am retrieving an array of result objects from a query and I am trying to write a function that will retrieve the properties of all the objects. So I am looping over the array and then using the properties from each result object. In an attempt to make the function more generic, I thought passing in a property name would be useful. I do a check before trying to retrieve a property to see that it exists, and it does:

 

if(property_exists($result, $value) && property_exists($result, $display)){
print("<option value=\"$result->$value\" >$result->$display</option>");
}

 

but this outputs '->$display' in the select options. And I also get the PHP error:

 

"Object of class stdClass could not be converted to string"

 

What is the obvious thing I am missing here?

 

Thanks,

Stephen

Link to comment
Share on other sites

Hi, I think it is how you are outputting, PHP can be very unusual at times, try this instead:

 

if(property_exists($result, $value) && property_exists($result, $display))
{
print('<option value="' . $result->$value . '" >' . $result->$display '</option>');
}

 

Also you should consider using get and set to return false when the property of an object doesn't exist (just ensures you wont get a fatal error).

 

On a side note, use echo instead of print (it's slightly faster)!

 

 

Link to comment
Share on other sites

Don't mix in HTML this way. It's always a bad idea and makes your code much more confusing to read.

 

<?php

class A {
   public $a = "1";
   public $b = "2";
}

$result = new A();
$value = a;
$display = b;

print_r( $ret =array($result->$value, $result->$display));

print '<option value="' . $ret[0] . '">' . $ret[1] . '</option>';
?>

 

Link to comment
Share on other sites

The advantage is separating logic from presentation.

 

In this example, I would wrap that tidbit in a function and return the array.

 

A procedural approach


<?php

//some-include-file.inc.php

function get_option($obj, $value, $display) {
    return array($obj->$value, $obj->$display);
}

//some-display-file.php
$result = new SomeObject();
$ret = get_option($result, $value, $display);
print '<option value="' . $ret[0] . '">' . $ret[1] . '</option>';

 

An OO approach would be harder to template for you without more detail.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.