NotionCommotion Posted November 1, 2016 Share Posted November 1, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/302442-using-php-objects-in-strings/ Share on other sites More sharing options...
Barand Posted November 1, 2016 Share Posted November 1, 2016 Use braces $string2="foo: {$obj->p2->p21}"; Quote Link to comment https://forums.phpfreaks.com/topic/302442-using-php-objects-in-strings/#findComment-1538843 Share on other sites More sharing options...
NotionCommotion Posted November 1, 2016 Author Share Posted November 1, 2016 Thanks Barand, but I knew the solution, but not the "why". I searched the docs and I couldn't find why it happens or were it is documented. Quote Link to comment https://forums.phpfreaks.com/topic/302442-using-php-objects-in-strings/#findComment-1538844 Share on other sites More sharing options...
Solution Barand Posted November 1, 2016 Solution Share Posted November 1, 2016 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 Quote Link to comment https://forums.phpfreaks.com/topic/302442-using-php-objects-in-strings/#findComment-1538846 Share on other sites More sharing options...
requinix Posted November 1, 2016 Share Posted November 1, 2016 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. Quote Link to comment https://forums.phpfreaks.com/topic/302442-using-php-objects-in-strings/#findComment-1538850 Share on other sites More sharing options...
NotionCommotion Posted November 2, 2016 Author Share Posted November 2, 2016 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? Quote Link to comment https://forums.phpfreaks.com/topic/302442-using-php-objects-in-strings/#findComment-1538863 Share on other sites More sharing options...
Jacques1 Posted November 2, 2016 Share Posted November 2, 2016 String interpolation in PHP is piss-poor, so either stop using it for anything other than plain variables, or get ready to dive into the parser. Quote Link to comment https://forums.phpfreaks.com/topic/302442-using-php-objects-in-strings/#findComment-1538865 Share on other sites More sharing options...
NotionCommotion Posted November 2, 2016 Author Share Posted November 2, 2016 String interpolation in PHP is piss-poor, so either stop using it for anything other than plain variables, or get ready to dive into the parser. By diving into the parser, you mean the source code or something else? Quote Link to comment https://forums.phpfreaks.com/topic/302442-using-php-objects-in-strings/#findComment-1538868 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.