So I have a class that looks like this (example)
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);
}
}
I have the following variables to be able to be called upon in every function within my class.
$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;
}
What is the best practice to assigning variables to the whole class?
This is a Code Igniter controller by the way
I'm guessing that you with "whole class" mean it's full scope. Basically having a public field:
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
I'm not certain I understand what you are asking, but if it's what I think you mean, you use 'static':
class Statistics extends Controller {
static $startdate;
and then you refer to it in a method as
self::$startdate;
精彩评论