0

I've been googling on this issue for a quite while and still haven't found a satisfactory answer yet :(

It's a small web-based online game where a player will have a game board and a resource board. What is the best way to include those two classes in the player class to create a has-relationship?

Is there a particular way to do it in CI? or just go with include()?

4

1 回答 1

1

另一种方法是依赖注入

class userModel extends CI_Model
{
      public function __construct(gameModel $gameModel)
      {
         var_dump($gameModel instanceof gameModel);
      }
}

-

class Controller extends CI_Controller
{
     public function method()
     {
         $this->load->model('gameModel');
         // load model with dependancy on gameModel
         // or vise-verse
         $this->load->model('userModel', new gameModel);
     }
}

-

或者就像我在评论中提到的

class userModel extends CI_Model
{
    public function method()
    {
       $gameModel = $this->load->model('gameModel');
    }

    public function __get($object)
    {
       $instance =&get_instance();
       return $instance->$object;
    }
}
于 2013-05-27T17:18:58.307 回答