0

I have come across this a few times, but have never used or understood why it happens or what it's actually for.

In some APIs i have used that return an object, converted then from JSON to a PHP object, some of the parameter names have an @ symbol in front of them.

A normal object would generally be like:

Object{
    param:'Hello'
}

But in some cases it's like this:

Object{
    @param:'Hello'
}

So what is the @ for and how did it get there? Never really come across this while coding myself except from some external api data.

Glen Elkins
  • 867
  • 9
  • 30
  • Folks can name their parameters anything. Just means the originating JSON is `{"@param":"Hello"}`. `{"@@@@@@!":"Hello"}` would be just as valid. Stupid, but valid. Ask the folks who made the API - maybe it means something specific to their API, but it has no formal meaning for PHP/JSON. – ceejayoz Aug 04 '17 at 13:39
  • 1
    Just another parameter name, the symbol can also be used to [silence error reporting](http://php.net/manual/en/language.operators.errorcontrol.php) but don't do that... please. – Mikey Aug 04 '17 at 13:40
  • ok, rather strange way of doing it, seems really pointless and just makes handling it on the other end more difficult. It makes more sense to access via $object->param than $object->{'@param'} – Glen Elkins Aug 04 '17 at 13:40
  • oh yeh i know it is used for silencing errors, but in this particular example it seems totally pointless – Glen Elkins Aug 04 '17 at 13:41
  • @GlenElkins Yeah, it's poorly chosen. Perhaps it means something like "this is a deprecated param" or "this parameter can't be edited" or whatnot, but that's entirely up to the folks running the API. – ceejayoz Aug 04 '17 at 13:42
  • 1
    thanks guys , made me feel better now lol – Glen Elkins Aug 04 '17 at 13:42
  • 1
    It can be used at times to emulate the way that XPath handles attributes, so in XML it may be `` XPath uses `@href` to refer to the attribute. – Nigel Ren Aug 04 '17 at 14:27

0 Answers0