Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/267.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 将变量分配给整个类[最佳实践?]_Php_Variables_Codeigniter - Fatal编程技术网

Php 将变量分配给整个类[最佳实践?]

Php 将变量分配给整个类[最佳实践?],php,variables,codeigniter,Php,Variables,Codeigniter,所以我有一个类,看起来像这样(示例) 我的类中的每个函数都可以调用以下变量 $affid = $this->session->userdata('affid'); $startdate = preg_replace('/[^\d-]+/', '', $this->input->get_post('start')); $enddate = preg_replace('/[^\d-]+/', '',$this->input->get

所以我有一个类,看起来像这样(示例)

我的类中的每个函数都可以调用以下变量

        $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;
    }
将变量分配给整个类的最佳实践是什么


顺便说一句,这是一个代码点火器控制器

我不确定我是否理解您的要求,但如果我认为您的意思是,您可以使用“静态”:

  class Statistics extends Controller {
    static $startdate;
然后在一个方法中将其称为

  self::$startdate;

我猜你的“全班”意味着它是全范围的。基本上有一个公共领域:

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

“这样改变整个问题可不好。”罗宾·奥赫登同意。将问题回滚到其原始版本@DevNull,如果要删除问题,请使用“删除”链接。虽然我不确定你为什么要删除一个有效的问题。@David Thomas:根据他的个人资料,上面写着“未注册用户”,但我认为他们看不到删除链接。@BoltClock:ah;可能就是这样。尽管如此,不必要地删除问题似乎非常不礼貌。嗯,往后退。再一次。我想这可能值得版主介入?我想问的是如何在构造函数中分配变量,然后能够在控制器类的其他函数中引用它们。我已经更新了我的答案。GetStartMethod现在检索(并返回)构造函数中设置的日期/时间值。如果您觉得这已经回答了您的问题,那么请关闭此问题并接受答案。如果您的意图是变量应该只在类的范围内,那么它可能应该是私有的。这也为公共GetStartDate()函数提供了用途。我同意。只是早些时候更新了它,以适应DevNull请求的更改。但我又更新了答案。这次StartDate变量只能在类范围内写入,并且可以通过GetStartDate方法读取和公开。
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