Solved

Dynamic object property help

Posted on 2011-02-27
3
240 Views
Last Modified: 2012-05-11
Hi,

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.
0
Comment
Question by:TwentyFourSeven
3 Comments
 
LVL 4

Expert Comment

by:DanDauchy
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:
http://www.php.net/serialize
http://www.php.net/unserialize

here's a similar forum posting:
http://stackoverflow.com/questions/2533093/store-objects-in-get
0
 
LVL 1

Author Comment

by:TwentyFourSeven
ID: 34991411
DanDauchy,

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....

http://example.com?mypage.php?type=cust
or
http://example.com?mypage.php?type=owner

etc etc.

0
 
LVL 3

Accepted Solution

by:
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:
hello
hello

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.
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Consider the following scenario: You are working on a website and make something great - something that lets the server work with information submitted by your users. This could be anything, from a simple guestbook to a e-Money solution. But what…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now