| Referencing an Object's attributes
|
MattyMoose

msg:4192077 | 5:32 pm on Aug 25, 2010 (gmt 0) | Hi! Long time no post... (Hi jk!) This is probably a really simple question, but I'm not familiar enough with PHP and how it works with objects. I'm experimenting with a CMS called concrete5. It seems pretty cool. Anyway, I'm calling a method within an object ("$c"), which returns what is effectively an array, but I'm not sure how ot specifically call the objects returned individually... Here's my code, and the output:
$surroundings = $c->getCollectionAttributeValue('trail_surroundings'); echo '<pre>' . print_r($surroundings) . '</pre>';
The print_r results in: SelectAttributeTypeOptionList Object ( [options:private] => Array ( [0] => SelectAttributeTypeOption Object ( [error] => [ID] => 1 [value] => Wooded [displayOrder] => 0 ) [1] => SelectAttributeTypeOption Object ( [error] => [ID] => 4 [value] => Rivers or Streams [displayOrder] => 3 ) [2] => SelectAttributeTypeOption Object ( [error] => [ID] => 5 [value] => Open Fields [displayOrder] => 4 ) ) [error] => ) |
| If I try to do something fancy like echo $surroundings[0][ID], I get: | Fatal error: Cannot use object of type SelectAttributeTypeOptionList as array |
| . Any hints? :)
|
Sandro87

msg:4192087 | 5:48 pm on Aug 25, 2010 (gmt 0) | I'm not sure but maybe "->" is the right sintax
|
MattyMoose

msg:4192097 | 5:59 pm on Aug 25, 2010 (gmt 0) | Ya, that didn't do it for me. I've had the bright idea of asking in the c5 forums as well, since I realized there's probably a function already built to do this! Thanks for your help. The question still stands, though, for my own learning/understanding - if I had this object, how would I reference the properties?
|
penders

msg:4192224 | 10:50 pm on Aug 25, 2010 (gmt 0) | In order to access the part you are after it looks like you would do something like: echo $surroundings->options[0]['ID']; BUT, the options property is declared private... [options:private] So, it is not directly accessible from outside the object! You will need to call a method of the object in order to access this property - which might not exist, or return the value you are expecting - print_r() does not show methods, just object properties.
|
astupidname

msg:4192385 | 7:33 am on Aug 26, 2010 (gmt 0) | A quick consult of the documentation [concrete5.xross-cube.com...] reveals the methods available within a SelectAttributeTypeOptionList Object, which is what $surroundings is. So it would appear you could do something like: for ($i = 0, $len = $surroundings->count(); $i < $len; $i++) { $sato = $surroundings->get($i); //a SelectAttributeTypeOption Object (thus the s a t o) echo 'id = '.$sato->ID.', value = '.$sato->value.', displayOrder = '.$sato->displayOrder.'<br>'; } |
|
|
|
|