title: Understanding the passing of data/life of a script in web development/CodeIgniter
body: <p>I hope I worded the title accurately enough but I typically use Java and don't have much experience in Web Development/PHP/CodeIgniter. I have a difficult time understanding the life cycle of a script as I found out trying to implement a certain feature to a website I am developing (as a means of learning how to). I'll first describe the feature I tried implementing and then the problem I ran into that made me question my fundamental understanding of how scripts work since I'm used to typical OOP.</p>
  
  <p>Ok so here goes...</p>
  
  <p>I have a webpage that has 2 basic tasks a user can do, create and delete an entry. What I attempted to implement was a way to time a user how long it takes them to complete a certain task. The way I did this was have a homepage where there would be a list of tasks a user to choose from (in this case 2, create and delete). A user would click a task which would link to the 'true' homepage where the user then would be expected to complete the task. My script looks like this:</p>
  
  <pre><code>&lt;?php
  
  class Site extends CI_Controller {
  
  var $task1;
  var $tasks = array(
                          "task1" =&gt; NULL,
                          "date1" =&gt; 0,
                          "date2" =&gt; 0,
                          "diff" =&gt; 0);
  
  function __construct()
  {
      parent::__construct();
      include 'timetask.php';
      $this-&gt;task1 = new TimeTask("create");      
  }   
  
  function index() 
  {   
      $this-&gt;tasks['task1'] = $this-&gt;task1-&gt;getTask();
      $this-&gt;tasks['diff'] = $this-&gt;task1-&gt;getTimeDiff();
  
      if($this-&gt;tasks['diff'] == NULL)
      {
              $this-&gt;tasks['diff'] = 0;
      }   
  
      $this-&gt;load-&gt;view('usability_test', $this-&gt;tasks);
  }
  
  function origIndex() 
  {   
      $this-&gt;task1-&gt;setDate1(new DateTime());
      $this-&gt;tasks['date1'] = $this-&gt;task1-&gt;getDate1()-&gt;getTimestamp();
      $data = array();
  
      if($q = $this-&gt;site_model-&gt;get_records())
      {
          $data['records'] = $q;
      }
  
      $this-&gt;load-&gt;view('options_view', $data);
  }
  
  function create()
  {
      $this-&gt;task1-&gt;setDate2(new DateTime());
      $this-&gt;tasks['date2'] = $this-&gt;task1-&gt;getDate2()-&gt;getTimestamp();
  
      $data = array(
              'author' =&gt; $this-&gt;input-&gt;post('author'),
              'title' =&gt; $this-&gt;input-&gt;post('title'),
              'contents' =&gt; $this-&gt;input-&gt;post('contents')
      );  
  
      $this-&gt;site_model-&gt;add_record($data);
      $this-&gt;index();
  
  }
  </code></pre>
  
  <p>I only included create to keep it short. Then I also have the TimeTask class, that actually another StackOverflow so kindly helped me with:</p>
  
  <pre><code>&lt;?php
  
  class TimeTask
  {
  private $task;
  
  /**
   * @var DateTime
   */
  private $date1, $date2;
  
  function __construct($currTask) 
  {
      $this-&gt;task = $currTask;
  }
  
  public function getTimeDiff() 
  {
      $hasDiff = $this-&gt;date1 &amp;&amp; $this-&gt;date2;
      if ($hasDiff) {
          return $this-&gt;date2-&gt;getTimestamp() - $this-&gt;date1-&gt;getTimestamp();
      } else {
          return NULL;
      }
  }
  
  public function __toString() 
  {
      return (string) $this-&gt;getTimeDiff();
  }
  
  /**
   * @return \DateTime
   */
  public function getDate1()
  {
      return $this-&gt;date1;
  }
  
  /**
   * @param \DateTime $date1
   */
  public function setDate1(DateTime $date1)
  {
      $this-&gt;date1 = $date1;
  }
  
  /**
   * @return \DateTime
   */
  public function getDate2()
  {
      return $this-&gt;date2;
  }
  
  /**
   * @param \DateTime $date2
   */
  public function setDate2(DateTime $date2)
  {
      $this-&gt;date2 = $date2;
  }
  
  /**
   * @return get current task
   */
   public function getTask()
   {
          return $this-&gt;task;
   }
  
  }
  
  ?&gt;
  </code></pre>
  
  <p>I don't think posting the views is necessary for the question but here is atleast how the links are made.</p>
  
  <pre><code>&lt;?php echo form_open('site/create');?&gt;
  ...and...
  &lt;?php echo anchor("site/delete/$row-&gt;id", $row-&gt;title); ?&gt;
  </code></pre>
  
  <p>Now there's no error in the code but it doesn't do what I expect of it and the reason I assume why is because that each time a function of the script is called via a new page it is NOT the same instance of the script called previously so any previously created objects are no longer there. This confuses me and leaves me quite unsure of how to implement this gracefully. Some ways I would guess of how to do this is by passing the necessary data through the URL or have data saved in a database and retrieve it later to compare the times. What would be a recommended way to do, not just this, but anything that needs previously created data? Also, am I correct to think that a script is only 'alive' for one webpage at a time?
  Thanks!</p>