homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

Referencing an Object's attributes

10+ Year Member

Msg#: 4192075 posted 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? :)



5+ Year Member

Msg#: 4192075 posted 5:48 pm on Aug 25, 2010 (gmt 0)

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


10+ Year Member

Msg#: 4192075 posted 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?


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

Msg#: 4192075 posted 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.


5+ Year Member

Msg#: 4192075 posted 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>';

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved