0

I have a simple associative array declared like the following:

static private $foo = [
        16 => 'xyz',
        7 => 'x',
        8 => 'y',
        9 => 'xy'
];

When I run a syntax check on this declaration, I get the following:

Parse error: syntax error, unexpected '16' (T_LNUMBER), expecting ']

What am I doing wrong here?

randombits
  • 47,058
  • 76
  • 251
  • 433
  • 2
    You're missing the last single quote after the 9 => 'xy' and the static and private keywords are the wrong way around. – Ben Swinburne Jul 13 '15 at 19:54
  • sorry that was just a typo when I pasted into here. the single quote is actually there in the real code. – randombits Jul 13 '15 at 19:55
  • 2
    No worries. Swap static and private so you have `private static $foo` – Ben Swinburne Jul 13 '15 at 19:55
  • Tried that as well @BenSwinburne unfortunately no cigar – randombits Jul 13 '15 at 19:56
  • 1
    `private static`- `static private` make no difference to the parser ;-) Since this is obviously part of a class definition can you please post a complete script? Try to create the smallest yet complete script possible that exhibits the behaviour you've described. – VolkerK Jul 13 '15 at 19:58
  • Perhaps post a bit more of your code? http://codepad.viper-7.com/0rk42d works using the above. – Ben Swinburne Jul 13 '15 at 19:59

5 Answers5

6

There are some weird characters (probably BOMs, I wasn't checking it) at the start of each line before an array element (see http://3v4l.org/ZnqIp). These characters are invisible, so their presence can cause strange errors like this one.

They show when pasted into Notepad++ as small squares:

Image showing characters in Notepad++

I removed them, try this code:

<?php

class Foo {
    static private $foo = [
        16 => 'xyz',
        7 => 'x',
        8 => 'y',
        9 => 'xy'
    ];
}

It should work just fine (see also http://3v4l.org/WQGWA).

Kubo2
  • 331
  • 3
  • 16
3

If you take a look at the copy/pasted code in any hex editor you'll notice strange invisible characters which are confusing PHP. Just do a clean copy/paste and you are good to go.

And while you are at it, check your editor configuration/encoding.

EDIT: A simple Unicode lookup says its the so called EN SPACE character (hex sequence E2 80 82). How you managed to put it in your code is beyond me.

enter image description here

1

I just tried out with copy&paste your code. For some reason I got the same error. Then deleted all whitespaces in array and again added spaces. Now everything worked out. Have no clue why thats happening. Just try to define array like

 private static $foo = [16 => 'xyz', 7 => 'x', 8 => 'y', 9 => 'xy'];

And if that works out - add line breaks. Some copy & paste magic is messing up things.

Endijs
  • 550
  • 3
  • 7
0

I think I figured out what is going on with your code. It has nothing to do with static private.

I copied your code above and ran a test and was getting the same parse error. I restructured your array and it seems you added spaces and not tabs. I'm relatively new to PHP but, I replaced spaces with tabs and it seems to work.

private static $foo = [
    16 => 'xyz',
    7 => 'x',
    8 => 'y',
    9 => 'xy'
];
ja408
  • 798
  • 5
  • 16
0

Try this

private static $foo = array( 
  16 => 'xyz', 
  7 => 'x', 
  8 => 'y', 
  9 => 'xy'
);
georgecj11
  • 1,600
  • 15
  • 22