Pete Jodo
Pete Jodo

Reputation: 463

CodeIgniter PHP accessing variable declared in a class from function in same class

I'm trying to access an array declared in a class from a function within the same class. I've attempted a couple different ways to try to make it work but I'm relatively new to PHP. This is a snippet of my code

class Site extends CI_Controller {

    var $dates = array(
        "Task" => NULL,
        "Date1" => NULL,
        "Date2" => NULL,
        "TimeDiff" => NULL
    );

function index() 
{   
    if($this->$dates['Date1'] != NULL && $this->$dates['Date2'] != NULL)
    {
        $this->$dates['TimeDiff'] = $this->$dates['Date2']->getTimestamp() - $this->$dates['Date1']->getTimestamp();            
    }

    $this->load->view('usability_test', $this->$dates);
}

I also attempted using the global keyword as such

global $dates;

And I still receive a "Undefined variable" error regardless. Thanks!

Upvotes: 1

Views: 5545

Answers (2)

hakre
hakre

Reputation: 197684

Create yourself a helper class that does what you need here:

class MyTask
{
    private $task;

    /**
     * @var DateTime
     */
    private $date1, $date2;

    ...

    public function getTimeDiff() {
        $hasDiff = $this->date1 && $this->date2;
        if ($hasDiff) {
            return $this->date2->getTimestamp() - $this->date1->getTimestamp();
        } else {
            return NULL;
        }
    }
    public function __toString() {
        return (string) $this->getTimeDiff();
    }

    /**
     * @return \DateTime
     */
    public function getDate1()
    {
        return $this->date1;
    }

    /**
     * @param \DateTime $date1
     */
    public function setDate1(DateTime $date1)
    {
        $this->date1 = $date1;
    }

    /**
     * @return \DateTime
     */
    public function getDate2()
    {
        return $this->date2;
    }

    /**
     * @param \DateTime $date2
     */
    public function setDate2(DateTime $date2)
    {
        $this->date2 = $date2;
    }
}

Key point here is that all the details with that range and stuff is inside the class. So you don't need to care elsewhere.

As additional bonus, the __toString method helps you to easily integrate this object in your views, because you can just echo objects then.

class Site extends CI_Controller
{
    /**
     * @var MyTask
     */
    private $dates;

    public function __construct() {
        $this->dates = new MyTask();
        parent::__construct();
    }

    function index() 
    {
        $this->load->view('usability_test', $this->$dates);
    }

    ...

Better?

Upvotes: 3

Colin Brock
Colin Brock

Reputation: 21565

You want $this->dates['Date1'] instead of $this->$dates['Date1']. Notice the absence of the $ before dates.

As a side note, be sure you're properly extending CI_Controller by defining the __construct()like this:

class Site extends CI_Controller {

    // class properties, etc.

    function __construct(){
        parent::__construct();
    }

    // class methods, etc.

}

Another thing to note, var is deprecated as of PHP5. You'll want to use either public, private, or protected depending on your needs (Edit: assuming, of course, that you are using PHP5).

Upvotes: 9

Related Questions