如何在 codeigniter 中使用 stdclass 调用 class 中的函数

How can I call a function in a class using stdclass in codeigniter

我是 CodeIgniter 的新手。我有一个与控制器功能相关的问题。控制器中的索引函数如下所示。

    public function index()
    {
        $data                   = new stdClass;
        $data->words            = "Apple";
        $data->shuffled_words   = str_shuffle("Apple");

        $this->load->view('t/header');
        $this->load->view('projects/game', $data);
        $this->load->view('t/footer');
    }

但是我在两个函数中写了索引函数。有一些错误没有奏效。

        public function index()
        {
            $this->shuffle();
            $this->load->view('t/header');
            $this->load->view('projects/game', $data);
            $this->load->view('t/footer');
        }

        public function shuffle()
        {
             $data                  = new stdClass;
             $data->words           = "Apple";
             $data->shuffled_words  = str_shuffle("Apple");
        }

你能帮我解决这个问题吗?

这是一个快速的解决方案。希望对你有所帮助。

    public function index()
    {
        $data = $this->shuffle();
        $this->load->view('t/header');
        $this->load->view('projects/game', $data);
        $this->load->view('t/footer');
    }

    public function shuffle()
    {
         $data                  = new stdClass;
         $data->words           = "Apple";
         $data->shuffled_words  = str_shuffle("Apple");

         return $data;
    }

$data 在索引函数中不可用。 这样使用

 public function index()
    {
        $data=$this->shuffle();//make $data available here
        $this->load->view('t/header');
        $this->load->view('projects/game', $data);
        $this->load->view('t/footer');
    }

    public function shuffle()
    {
         $data                  = new stdClass;
         $data->words           = "Apple";
         $data->shuffled_words  = str_shuffle("Apple");
         return $data;//return the values
    }