代码之家  ›  专栏  ›  技术社区  ›  TheRealPapa

PHP数组不会分配键值

php
  •  -2
  • TheRealPapa  · 技术社区  · 9 年前

    更新:以下是完整控制器方法:

    protected $game = [];
    
    private function stageAction($type, $actions)
    {
        if (env('APP_DEBUG')) {
            Log::info('STAGE ACTION started with $type= '.$type.' *********************');
        }
    
        $message                    = [];
        $this->game['sys_msg']      = [];
        $this->game['stage_action'] = [];
    
        switch ($type) {
    
            case 'attack_end':
                $message['action']  = 'ignore';
                $message['message'] = '<b>'.active_player()['nickname'].'</b> ends their attack.';
                $this->game['sys_msg'] = $message;
                break;
    
            case ......
                break;
    
            default:
        }
    
        // If it is an "ignored" message then send it now
        if ($this->game['sys_msg']['action'] == 'ignore') {
            $this->broadcastSysMsg();
        }
    
        if (env('APP_DEBUG')) {
            Log::info('SYS MSG - '.print_r($this->game['sys_msg'], true));
        }
    

    protected $game = [];
    

    在其中一种方法中,我添加了一个键,如下所示:

    $this->game['sys_msg'] = [];
    

    然后我给它添加如下值:

    $message['action']  = 'ignore';
    $message['message'] = 'My message is blah blah blah...';
    $this->game['sys_msg'] = $message;
    

    Log::info('SYS MSG - '.print_r($this->game['sys_msg'], true));
    

    但我明白了:

    []
    

    我想给这个值分配一个对象数组。为什么这不起作用,但没有失败?

    2 回复  |  直到 9 年前
        1
  •  2
  •   HostFission    9 年前

    有几个问题,你没有忘记 $message

    $message = [
      'action'  => 'ignore',
      'message' => 'My message is blah blah blah...'
    ];
    

    您还可以进一步将其直接分配给 game

    $this->game['sys_msg'] = [
      'action'  => 'ignore',
      'message' => 'My message is blah blah blah...'
    ];
    

    至于你没有看到更新后的值,可能是由很多事情引起的,请发布一个完整的代码示例。

    编辑:

    不要像现在这样使用数组,请为 sys_msg ,这样您就不会有打字错误的风险,例如 aCtion 一只奇怪的虫子。您还可以确保将其初始化为已知状态。例如:

    class SysMsg
    {
      const ACTION_IGNORE  = 0;
      const ACTION_PROCESS = 1;
    
      protected $action;
      protected $message;
    
      public function __construct($action = self::ACTION_IGNORE, $message = null)
      {
        $this->action  = $action;
        $this->message = $message;
      }
    
      public function getAction () { return $this->action; }
      public function getMessage() { return $this->message; }
    }
    
    class SysMsg_AttackEnd extends SysMsg
    {
      protected $player;
    
      public function __construct($player)
      {
        parent::__construct(SysMsg::ACTION_IGNORE, "");
        $this->player = $player;
      }
    
      public function getMessage()
      {
        return '<b>' . $this->player['nickname'] . '</b> ends their attack.';
      }
    }
    
    
    private function stageAction($type, $actions)
    {
      switch($type)
      {
        case 'attack_end':
          $msg = new SysMsg_AttackEnd(active_player());
          break;        
      }
    
      if (isset($msg) && $msg->getAction() == SysMsg::ACTION_IGNORE)
      {
        $this->game['sys_msg'] = $msg;
        $this->broadcastSysMsg();
      }
    }
    
        2
  •  2
  •   Ramadhan    9 年前

    我能知道这个密码吗

    $this->game['sys_msg'] = [];
    

    $message['action']  = 'ignore';
    $message['message'] = 'My message is blah blah blah...';
    $this->game['sys_msg'] = $message;
    
    推荐文章