0

The problem is that when using PHP7 the expected result is different from PHP5, see following example with $object2.

I think that the three methods are equivalent from each other, but it seems not to be true.

It seems that in PHP7 the shortcut used with $object2 return the type of variable instead of the value.

Is there some environment configuration of PHP7 that makes it behave like PHP5 with respect to this issue?

$array1 = array();
$array1["Key"] = "Value"; 
$object1 = new stdClass();
$key1 = $array1["Key"];
$object1->Stream = new stdClass();
$object1->Stream->$key1 = 5;
echo json_encode($object1);

$array2 = array();
$array2["Key"] = "Value"; 
$object2 = new stdClass();
$object2->Stream = new stdClass();
$object2->Stream->$array2["Key"] = 5;
echo json_encode($object2);

$array3 = array();
$array3 = "Value"; 
$object3 = new stdClass();
$object3->Stream = new stdClass();
$object3->Stream->$array3 = 5;
echo json_encode($object3);

Result in PHP5:

{"Stream":{"Value":5}}

{"Stream":{"Value":5}}

{"Stream":{"Value":5}}

Result in PHP7:

{"Stream":{"Value":5}}

{"Stream":{"Array":{"Key":5}}}

{"Stream":{"Value":5}}
1
  • BTW: For developing, turn on notices, then you would see, that PHP emmits a notice: Notice: Array to string conversion in ... on line ... Commented Oct 31, 2017 at 9:18

2 Answers 2

1

Try wrapping it in {}

$object2->Stream->{$array2["Key"]} 
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the solution. Anyway I'm trying not to change my actual code of $object2 implementation because this is an abstract concept that I have used extensively on a big project.
When you do not want to change code, then don't ask questions, how to modify you code, to get the desired result...
My final question was "Is there some environment configuration of PHP7 that makes it behave like PHP5 with respect to this issue?". thanks for your support.
I don't know of any setting that will change the behaviour for php7. I am surprised php5 treats it differently to php7. Perhaps ask on the php website
0

You can Store array index in varaible then use this variable in object

Try follwoing code

$array1 = array();
$array1["Key"] = "Value"; 
$object1 = new stdClass();
$key1 = $array1["Key"];
$object1->Stream = new stdClass();
$object1->Stream->$key1 = 5;
echo json_encode($object1);

$array2 = array();
$array2["Key"] = "Value"; 
$tmp = $array2["Key"];
$object2 = new stdClass();
$object2->Stream = new stdClass();
$object2->Stream->$tmp = 5;
echo json_encode($object2);

$array3 = array();
$array3 = "Value"; 
$object3 = new stdClass();
$object3->Stream = new stdClass();
$object3->Stream->$array3 = 5;
echo json_encode($object3);

1 Comment

Your solution is the same that I have used in $object1 example. I'm searching a solution without changing the actual code of my $object2 implementation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.