Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Unexpected "=>", is it really unexpected?

Comments in 'Plugin Development' started by Primus, Mar 18, 2016.

  1. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    PHP:
      /** @var array $sessionData */
       
    protected $sessionData = array (
         
    "password" => "",
         
    "lastJoined" => "",
         
    "kills" => 0,
         
    "deaths" => 0,
         
    "coins" => 0,
         
    "xp" => 0,
         
    "level" => 1,
         
    "online" => true,
         
    "alive" => true,
         
    "clan" => "",
         
    "rank" => "",
         
    "isAuthenticated" => false,
         
    "isRegistered" => false,
       );
       
    /*
       * Other plugins like SupremeBlockHunt or SupremeSkyWars can add more sessionData than you can see above
       */

       
    public function __construct(SupremeCore $pluginPlayer $player){
         
    $this->player $player;
         
    $this->plugin $plugin;
       }

       
    /**
       * Updates session data, this function is usually called on player join to rewrite default sessionData
       * Most sessionData values are updated on certain events.
       */
       
    public function updateSession(){
         
    $data $this->getPlugin()->getDataProvider()->getPlayer($this->player);

         
    $this->sessionData[
           
    "password" => $data['password'], // 50 Line
           
    "kills" => $data['kills'],
           
    "deaths" => $data['deaths'],
           
    "coins"   => $data['coins'],
           
    "xp" => $data['xp'],
           
    "level"   => $data['level'],
           
    "online" => $this->player->isConnected(),
           
    "alive" => $this->player->isAlive(),
           
    "clan" => $data['clan'],
           
    'rank' => $data['rank'],
           
    'isAuthenticated' => $this->getPlugin()->isAuthenticated($this->player),
           
    'isRegistered' => $this->getPlugin()->isRegistered($this->player)
         ];
       }
    Error
    Code:
    [21:48:18] [Server thread/CRITICAL]: "Could not pass event 'pocketmine\event\player\PlayerJoinEvent' to 'SupremeCore v1.0.0': syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ']' on sc\Handler\PlayerEventListener
    [21:48:18] [Server thread/CRITICAL]: ParseError: "syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ']'" (EXCEPTION) in "/SupremeCore/src/sc/Utils/Session" at line 50
    
    Is this something related to PHP7 bugs?
  2. MichaelM04
    Offline

    MichaelM04 Active Member Plugin Developer

    Joined:
    Nov 12, 2015
    Posts:
    213
    Plugins:
    1
    Minecraft User:
    Michael
    Maybe...

    I've been running into a lot of "unexpected" errors recently when converting plugins to PHP7
    KubixQ and Primus like this.
  3. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    ** SOLVED **
    Alright, tried same code structure on PHP5.6 but still same problem. Maby my memory is cheating on me :confused:

    Solution I use
    PHP:
    $this->sessionData['key'] = 'value';
  4. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Nope my memory is loyal, for now.

    PHP:
    <?php
    $array 
    = array(
       
    "foo" => "bar",
       
    "bar" => "foo",
    );
    // as of PHP 5.4
    $array = [
       
    "foo" => "bar",
       
    "bar" => "foo",
    ];
    ?>
  5. Skullex
    Offline

    Skullex Active Member

    Joined:
    Nov 14, 2015
    Posts:
    212
    Minecraft User:
    Skullex
    Lol
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It is not related to PHP 7. It is wrong in the first place.
    $foo[$bar] means, to get the value in array $foo associated to key $bar.
    If you want to assign an array to a variable, it should be:
    PHP:
    $foo = [
      
    $a => $b,
      
    $c => $d
    ];
    Both work on all PHP versions since PHP 5.4 where short syntaxes have been added.

    It should be
    PHP:
    $foo = [$a => $b];
    not
    PHP:
    $foo[$a => $b];
    HotFireyDeath and PocketKiller like this.
  7. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Wouldn't that rewrite previous keys.
    PHP:
    $array = array(
     
    'foo' => 'bar',
     
    'choco' => 'late',
     
    'pan' => 'cake'
    );
    // Now $array has three keys in it
    $array = [
     
    'foo' => 'bar',
     
    'choco' => 'late',
    ];
    // Will 'pan' => 'cake' be in it?
  8. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Then use array_merge().

Share This Page

Advertisement