Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

3D objects in strings 1

Status
Not open for further replies.

progman1010

Programmer
Jan 2, 2008
108
US
Hello- this is more of a "why" question than a problem itself...

It's easy to create and reference 3-dimensional objects:
Code:
//example
$ob->firstkey->nextkey;

PHP syntax allows me to include a 2D object into a string without much trouble:
Code:
echo "Some text and $var->thisval more regular text";
// where $var->thisval is the object

but when I put a 3D object into a string like above:
Code:
echo "some text and $var->thiskey->thisval more text";
// where $val->thiskey->thisval is the object

the parser doesn't even give me an error- it just stops execution altogether! (yes, display errors is on and error_reporting is ALL) So what i get is a blank screen which is far less helpful.

Anyone know why?
 
it could be a memory leak but i'm just guessing in the dark here.

is anything reported in your php error logs? in your server logs?

if you enclose the variables in curly braces does it make a difference (this disambiguates)
Code:
echo "some text and {$var->thiskey->thisval} more text";
 
i should have added that useful info for us on this problem would include the server environment you are using plus php ver.
 
Until now, i hadn't tried it in a file all by itself, only within other scripts.

Without curly braces, it throws an error:
Catchable fatal error: Object of class stdClass could not be converted to string in /home/generic5/public_html/test.php on line 6

With curly braces, it executes fine.
 
the $var->thiskey->thisval

is thisval a property or a method of thiskey?

because the error appears to be saying that it is neither but is, in fact, an object itself.

this is consistent with your commented statement above
Code:
// where $val->thiskey->thisval is the object

but then that would mean that you were trying to echo an object. which will never work. you can print/echo the values of object properties and the return values of object methods. if you want to output an object itself you have to use print_r.

 
good questions- what I was trying to do is basically use the object like an array (like $val['thiskey']['thisval']) because of how my class was created.

Seems like I should give more thought to the object itself and its architecture.

Based on this, I guess thisval would be a property of thiskey, but it's not really what I was trying to do.

Now I have a new question: how should I have approached this if all I wanted was a bunch of text in an array. Is this right?
Code:
$val->thiskey['thisval'] = 'text';
 
a multi dimensional 'array' like this

Code:
$array['sub-array']['sub-sub-array'] = 4;

can be addressed as an anonymous object like so

Code:
$object->sub-object->sub-objectProperty = 4;

if i have not understood your question properly can you go back to the basics of what you are trying to achieve? perhaps also explain the context.
 
I think we're good here. It appears I wasn't so off-base after all. I was just trying to use the object type like a multi-dimensional array.

In context, the object is from a class like
Code:
$c = new member(23);
echo $c->firstname;
Then, there was a need to get an array within the member object:
Code:
foreach ($c->subscriptioninfo as $t)
   echo $t."<br>";
//or
echo $c->subscriptioninfo->renewed;
echo $c->subscriptioninfo->othervar;

You have been most helpful. Thanks.

-Jay
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top