Dynamic object property help

Posted on 2011-02-27
Last Modified: 2012-05-11

This works perfectly for me :

<?php echo $json_decoded->attributes->cust->org_name ?> 

Open in new window

However, if I try to make it dynamic.....

<?php echo $json_decoded->attributes->$_GET['type']->org_name ?> 

Open in new window

It fails with an "Trying to get property of non-object" error.
Question by:TwentyFourSeven

Expert Comment

ID: 34991390
From what I've read in order to pass an object through a GET you need to serialize it on the way in, and then unserialize it on the way out.

Here's the 2 php functions that can do that:

here's a similar forum posting:

Author Comment

ID: 34991411

I'm not exactly sure you've understood what I'm after ?  Either that, or your solution seems overly complex ?

I have a page that displays some data based on some decoded JSON.....

So :
<?php echo $json_decoded->attributes->cust->org_name ?>

Would display the organisation name ("org_name") for the customer ("cust").

The problem is, there are alternative objects on the same JSON, for example "owner"
<?php echo $json_decoded->attributes->owner->org_name ?>

So all I want to do is some code reuse, and be able to call one page with a different get....

etc etc.


Accepted Solution

dtocci earned 500 total points
ID: 34991975
I made a test.php page and used the following code:
$test = new stdClass();
$test->foo = new stdClass();
$test->foo->bar = "hello";

echo $test->foo->bar;
echo "<br/>";
echo $test->$_GET['type']->bar; 

Open in new window

When browsing to temp?type=foo, the output was:

So it seems like accessing an object by using a name sent through $_GET['type'] should work just fine.  So perhaps the problem is with the specific object or your querystring.  I'd var_dump them before the line of code that generates the error to make sure they are what you expect them to be.

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Introduction This article is intended for those who are new to PHP error handling (  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

820 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