Jump to content

using php objects in strings


Go to solution Solved by Barand,

Recommended Posts

Why does the second string definition cause an error?

<?php
ini_set('display_errors', 1);

$obj=new stdClass();
$obj->p1=123;
$obj->p2=new stdClass();
$obj->p2->p21=123;
$string="foo: $obj->p1";
$string="foo: $obj->p2->p21";

Catchable fatal error: Object of class stdClass could not be converted to string in /var/www/html/test2.php on line 9

 

 

Link to comment
https://forums.phpfreaks.com/topic/302442-using-php-objects-in-strings/
Share on other sites

  • Solution

I suspect it's because it uses "complex string syntax", just as $str = "ABC{$array[1][2]}"; requires the {..} whereas $str = "ABC$array[3]"; does not.

 

http://uk1.php.net/manual/en/language.types.string.php#language.types.string.parsing

Because that's all the "simple" syntax allows: one property. If you're looking for a rationale from the PHP developers as to why only one property indirection is permitted then you're probably going to have to ask them directly.

 

[edit] Ohhh, you mean why is it an error. Not why is it not allowed. Nevermind.

Because that's all the "simple" syntax allows: one property. If you're looking for a rationale from the PHP developers as to why only one property indirection is permitted then you're probably going to have to ask them directly.

 

[edit] Ohhh, you mean why is it an error. Not why is it not allowed. Nevermind.

 

Got a phone number?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.