Why does "scalar" appear like this when I cast json_decode() (in non-associative mode) to an object?

i336_

While doing some code refactoring I momentarily ended up in a situation where I was basically doing the (somewhat abstracted-out) equivalent of

$data = (object)json_decode('"test"');

Of course I understand json_decode() generates objects on its own unless assoc is false. (Incidentally I got into this situation because I was in the middle of moving some format processing code around, and I hadn't yet realized one of my (object) casts was now redundant.)

But... when this happened, PHP decided that $data contained:

stdClass Object
(
    [scalar] => test
)

Wat.

scalar?!

Last I learned, "test" is a string, so it seems more than one pile of things has fallen over internally here. Or is this unintuitive yet intended design?!

I have of course removed the (object) and things work exactly how I intended now. So there's no bug here per se. I just wanted to understand what just happened.

Here you go, in case you want to join in the headscratching:

php -r 'print_r((object)json_decode("\"test\""));'

I'm using 7.0.25.

iainn

This is exactly what the manual specifies will happen when casting a scalar type (i.e. int, string, float, boolean) to an object.

If an object is converted to an object, it is not modified. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was NULL, the new instance will be empty. An array converts to an object with properties named by keys and corresponding values. Note that in this case before PHP 7.2.0 numeric keys have been inaccessible unless iterated.

For any other value, a member variable named scalar will contain the value.

$obj = (object) 'ciao';
echo $obj->scalar;  // outputs 'ciao'

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Laravel Error returns when it does json_decode

分類Dev

json_decode to array or to object

分類Dev

How to pass hash-like object which does associative role to a constructor expecting a hash?

分類Dev

Why does Moshi return a nullable object when parsing json with fromJson?

分類Dev

Why cast does not succeed?

分類Dev

Why do I get the error of unable to cast object

分類Dev

components in JFrame does not appear when i create a table

分類Dev

Why is json_decode output always null?

分類Dev

Why does this GridBagLayout not appear as planned?

分類Dev

why does 2 back slashes appear in python if I enter a back slash followed by a special character

分類Dev

Converting a JSON object into a Bash associative array

分類Dev

Why does an NSInteger variable have to be cast to long when used as a format argument?

分類Dev

Why does shift think it's receiving a parameter in scalar context?

分類Dev

ipython: Notebook does not appear to be JSON

分類Dev

Why does Ember complain when pushing an object onto an Ember array?

分類Dev

Why is it legal to cast an Object array to a generics array?

分類Dev

Why does passing an object instance to the UI thread like this cause delays/hang ups?

分類Dev

Cast an object to a type of float but it looks like struct System.Single

分類Dev

Props appear like undefined when a children is passing too

分類Dev

Why does one of my two Dell monitors unexpectedly enter power save mode when using DisplayPort daisychaining?

分類Dev

Why gcc does not give a warning or error when invalid mode is used in fopen()?

分類Dev

Why does ObjectOutputStream.readObject() return object of type Object and not object of type I wrote it in?

分類Dev

Why `this` returns Empty object when Compiled on regular IDE(like visual studio code)?

分類Dev

Why, when I convert a string into binary, does it miss the first zeros?

分類Dev

Why does the original array change when I change the copy?

分類Dev

Why does my thread stop when I work with UI

分類Dev

X is a variable but used like a type when trying to cast

分類Dev

How can I decode an object when original class is not available?

分類Dev

Unexpected indentation behaviour when I set the terminal to raw mode – why is this happening?

Related 関連記事

  1. 1

    Laravel Error returns when it does json_decode

  2. 2

    json_decode to array or to object

  3. 3

    How to pass hash-like object which does associative role to a constructor expecting a hash?

  4. 4

    Why does Moshi return a nullable object when parsing json with fromJson?

  5. 5

    Why cast does not succeed?

  6. 6

    Why do I get the error of unable to cast object

  7. 7

    components in JFrame does not appear when i create a table

  8. 8

    Why is json_decode output always null?

  9. 9

    Why does this GridBagLayout not appear as planned?

  10. 10

    why does 2 back slashes appear in python if I enter a back slash followed by a special character

  11. 11

    Converting a JSON object into a Bash associative array

  12. 12

    Why does an NSInteger variable have to be cast to long when used as a format argument?

  13. 13

    Why does shift think it's receiving a parameter in scalar context?

  14. 14

    ipython: Notebook does not appear to be JSON

  15. 15

    Why does Ember complain when pushing an object onto an Ember array?

  16. 16

    Why is it legal to cast an Object array to a generics array?

  17. 17

    Why does passing an object instance to the UI thread like this cause delays/hang ups?

  18. 18

    Cast an object to a type of float but it looks like struct System.Single

  19. 19

    Props appear like undefined when a children is passing too

  20. 20

    Why does one of my two Dell monitors unexpectedly enter power save mode when using DisplayPort daisychaining?

  21. 21

    Why gcc does not give a warning or error when invalid mode is used in fopen()?

  22. 22

    Why does ObjectOutputStream.readObject() return object of type Object and not object of type I wrote it in?

  23. 23

    Why `this` returns Empty object when Compiled on regular IDE(like visual studio code)?

  24. 24

    Why, when I convert a string into binary, does it miss the first zeros?

  25. 25

    Why does the original array change when I change the copy?

  26. 26

    Why does my thread stop when I work with UI

  27. 27

    X is a variable but used like a type when trying to cast

  28. 28

    How can I decode an object when original class is not available?

  29. 29

    Unexpected indentation behaviour when I set the terminal to raw mode – why is this happening?

ホットタグ

アーカイブ