Home > Software engineering >  Square brackets with a class name inside in PHP
Square brackets with a class name inside in PHP

Time:05-03

I'm debugging someone else old code, I think it has been made under php5 and I'm debugging after migrating to php8. The question I have is just about a syntax I haven't seen before, I don't know how to google and I can't test. I found this:

$aSelectedAssignmentFields=$advanced_options[assignment_fields];

And I don't know what it means. The value inside of the square brackets doesn't have a dollar sign or quotes. There is a class with that name but because of other issues I can't run it and check if that's right or wrong. It would be weird that is wrong, it would not that is outdated and in case it's right I would like to know what does it mean.

Thanks

CodePudding user response:

It's impossible to do more than guess without more context; and may be impossible to be certain without a time machine to go back and ask the original developer (if you asked them now, I doubt they'd remember). I can think of two possibilities:

  1. It was intended to be a constant, and the definition has got lost somewhere. Most people right constants in upper-case, but there's never been anything in PHP itself to enforce that.
  2. It was intended to be a string, as in $aSelectedAssignmentFields=$advanced_options['assignment_fields']; Prior to PHP 8, there was an odd "feature" where an unquoted name which didn't match a constant would implicitly act like a quoted string. In PHP 5, it would have given a Notice; in PHP 7, a Warning; and in PHP 8, the functionality was removed and you get an error.
  •  Tags:  
  • php
  • Related