所以我有一个看起来像这样的类(例子)
class Statistics extends Controller {
function __construct()
{
parent::Controller();
$data['title'] = 'Statistics';
//Locale
setlocale(LC_MONETARY, 'en_US');
date_default_timezone_set('America/Chicago');
//Require Login
$this->load->library('session');
if($this->session->userdata('affid'))
{
$this->load->view('userhead',$data);
}
else
{
header("Location: /network/login/submit");
}
}
//Index page. Show stat display options
function index()
{
$this->load->view('statoptions');
$this->load->view('footer');
}
//Stats by offer
function offer()
{
$this->load->library('statistics');
$this->_getvars();
if(!$getsort && $direction)
{
$sort = array('Stat.offer_id' => 'desc','Stat.revenue' => 'desc');
}
$stats = $this->statistics->aff_stats($affid,$startdate,$enddate,'Stat.offer_id',$sort);
$data['statdate'] = array('start' => $startdate, 'end' => $enddate);
$data['stats'] = $stats['data'];
$data['statview'] = 'offer';
$data['dir'] = 'desc';
if($direction == 'desc')
{
$data['dir'] = 'asc';
}
$this->load->view('statistics',$data);
#print_r($data['stats']);
}
//Stats by day
function daily()
{
$stats = $this->statistics->aff_stats($affid,$startdate,$enddate,'Stat.date',$sort);
$data['statdate'] = array('start' => $startdate, 'end' => $enddate);
$data['stats'] = $stats['data'];
$data['statview'] = 'daily';
$data['dir'] = 'desc';
if($direction == 'desc')
{
$data['dir'] = 'asc';
}
$this->load->view('statistics',$data);
#print_r($stats);
}
//Stats by Affiliate's SubID
function subid()
{
$stats = $this->statistics->aff_stats($affid,$startdate,$enddate,'Stat.affiliate_info1',$sort);
$data['statdate'] = array('start' => $startdate, 'end' => $enddate);
$data['stats'] = $stats['data'];
$data['statview'] = 'subid';
$data['dir'] = 'desc';
if($direction == 'desc')
{
$data['dir'] = 'asc';
}
$this->load->view('statistics',$data);
#print_r($stats);
}
}
我可以在班级的每个函数中调用以下变量。
$affid = $this->session->userdata('affid');
$startdate = preg_replace('/[^\d-]+/', '', $this->input->get_post('start'));
$enddate = preg_replace('/[^\d-]+/', '',$this->input->get_post('end'));
if(!$startdate)
{
$startdate = date("Y-m-d");
}
if(!$enddate)
{
$enddate = date("Y-m-d");
}
$getsort = htmlspecialchars($this->input->get_post('sort'),ENT_QUOTES);
$direction = htmlspecialchars($this->input->get_post('dir'),ENT_QUOTES);
if($getsort && $direction)
{
$sort[$getsort] = $direction;
}
将变量分配给整个班级的最佳做法是什么?
顺便说一下,这是一个Code Igniter控制器
答案 0 :(得分:2)
我猜你用“全班”意味着它的全部范围。基本上有一个公共领域:
public class StatisticsController extends Controller {
private $StartDate = null;
public __construct()
{
$this->StartDate = date(DATE_RFC822);
}
public function GetStartDate()
{
return $this->StartDate;
}
}
$controller = new StatisticsController();
echo($controller->GetStartDate()); // prints something like: Mon, 15 Aug 2005 15:12:46 UTC
答案 1 :(得分:0)
我不确定我明白你在问什么,但如果这是我认为你的意思,你会使用'静态':
class Statistics extends Controller {
static $startdate;
然后你在方法中引用它
self::$startdate;