Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Referencing an Object's attributes



5:32 pm on Aug 25, 2010 (gmt 0)

10+ Year Member

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? :)


5:48 pm on Aug 25, 2010 (gmt 0)

5+ Year Member

I'm not sure but maybe "->" is the right sintax


5:59 pm on Aug 25, 2010 (gmt 0)

10+ Year Member

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?


10:50 pm on Aug 25, 2010 (gmt 0)

WebmasterWorld Senior Member penders is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

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.


7:33 am on Aug 26, 2010 (gmt 0)

5+ Year Member

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>';

Featured Threads

Hot Threads This Week

Hot Threads This Month