点码器负载数组产生不同的功能



我有更多的函数,我需要多次读取$data['getContacts'],代码工作正常,但是有一种干净且不同的方法来调用它?

谢谢!

 class AppController extends CI_Controller {
        public $id;
        function __construct() {
           parent::__construct(); 
           $this->id =  !empty($this->input->post('id')) ? (int)$this->input->post('id', TRUE) : '';             
        }
           public function restoreCredit()
           {
               $data['getContacts'] = $this->appmodel->getContacts($this->id); //repeat here?
           if($data['getContacts']->status != false) :
                     $this->appmodel->restoreCredit($this->id);
           endif; 
           }
            public function createRandToken()
            {
                $data['getContacts'] = $this->appmodel->getContacts($this->id); //repeat here?
                    if(!empty($data['getContacts']) && $data['getContacts']->token == false): 
                      $this->appmodel->setRandUserToken($this->id);
                endif;  
            }
    }

你可以定义一个函数getContacts。它将第一次从数据库中获取$contacts,之后它将始终返回获取的联系人。

<?php
class AppController extends CI_Controller
{
    public $id;
    public $contacts;
    function __construct()
    {
        parent::__construct();
        $this->id = !empty($this->input->post('id')) ? (int) $this->input->post('id', TRUE) : '';
    }
    public function getContacts() {
        if( !empty ( $this->contacts) ) { //If its populated return from here.
            return $this->contacts;
        }
        $this->contacts = $this->appmodel->getContacts($this->id);
        return $this->contacts;
    }
    public function restoreCredit()
    {
        $data['getContacts'] = $this->getContacts();
        if ($data['getContacts']->status != false) :
            $this->appmodel->restoreCredit($this->id);
        endif;
    }
    public function createRandToken()
    {
        $data['getContacts'] = $this->getContacts();
        if (!empty($data['getContacts']) && $data['getContacts']->token == false) :
            $this->appmodel->setRandUserToken($this->id);
        endif;
    }
}

相关内容

  • 没有找到相关文章

最新更新