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

Weird error. Illegal offset 'name'

Comments in 'Plugin Development' started by Hotshot_9930, Mar 15, 2016.

  1. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    For example:
    PHP:
    class Foo{
      public 
    $bar1;
      public 
    $bar2;
      public 
    $bar3;
    }
    $foo = new Foo();
    for(
    $i 1$i <= 3$i++){
      
    $name "bar" $i;
      
    $foo->$name "bar $i";
      
    // same as:
      
    $foo->{"bar" $i} = "bar $i";
    }
    However, it is very poor practice to do this. The only advantage of hardcoding these fields rather than using an array is that it will use a fixed amount of memory, rather than creating an array that can link to an infinite number of entries.

    Nevertheless, you can also use SplFixedArray to create an array with fixed array size.
  2. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    Thought so, I don't see why this method exists, it's messy and confusing (not that programming itself isn't confusing :p).
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Actually, I had just been using this half an hour ago (before I came on and checked forums alerts).
    dynfield.PNG
  4. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    $gameFrozenStr is a string.
    PHP:
    $gameFrozenStr "gameFrozenFT" $gameId;
    So if the players game id is 1 then $gameFrozenStr would equal to gameFrozenFT1 which is a class property name in the plugin. It would work. @PEMapModder is right.
  5. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Thanks for all the replies guys but I have solved this problem. I have changed freeze() function:
    PHP:
     public function freeze(Player $player$gameId) {
      switch(
    $gameId) {
        case 
    1:
        
    $this->getPlugin()->gameFrozenFT1[$player->getName()] = $player;
        break;
     
        case 
    2:
        
    $this->getPlugin()->gameFrozenFT2[$player->getName()] = $player;
        break;
       
    // etc...
      
    }
    }
    It works now, but i have no idea why. Oh well, thanks again.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It is really better to change gameFrozenFT to an array, so you can access it like $this->getPlugin()->gameFrozenFT[1][$player->getName()]. It will be more efficient.
    This is also OK:

    PHP:
    $name "gameFrozenFT" $gameId;
    (
    $this->getPlugin()->$name)[$player->getName()] = $player;
    But again, use arrays. Don't put the index in the field name. This is not the proper way of using data structures.
    Hotshot_9930 likes this.

Share This Page

Advertisement