5

I'm doing something wrong and I can't figure out what to do about it (how to fix it)

code:

var_dump($each->promotion-type);

returns:

PHP Notice:  Use of undefined constant type - assumed 'type' in newfile.php on line 19

I can't change that variable name as that how I get from my vendor, any ideas how I can access that promotion-type variable? (syntax wise)

alexus
  • 7,256
  • 12
  • 44
  • 66
  • You get an *object* like that from your vendor? Where is that data coming from? Isn't it possible to use an array instead? – deceze Oct 19 '11 at 01:19
  • 1
    @deceze: For all you know it might have been extracted from JSON, and dashes are perfectly fine in JSON object keys... – BoltClock Oct 19 '11 at 01:21
  • @Bolt Exactly, in which case decoding it to an array would be more appropriate. – deceze Oct 19 '11 at 01:22
  • @deceze: data comes in XML, but looks like BoltClock answer my question, so thanks to all :) – alexus Oct 19 '11 at 01:22
  • See also [my attempt at a canonical answer for this issue](http://stackoverflow.com/questions/2941169/what-does-the-php-error-message-notice-use-of-undefined-constant-mean/8025500#8025500) – John Carter Nov 06 '11 at 06:23

1 Answers1

7

It's spitting that notice because the expression is being interpreted as variable $each->promotion minus constant type.

To access a property with the dash in its name, use curly braces and quotes:

var_dump($each->{'promotion-type'});
BoltClock
  • 700,868
  • 160
  • 1,392
  • 1,356