I am stumped or just gone brain dead.

I have a class that returns a single value for each function called. eg

class DateClass {

  function Get_Trans_TS() {
    $this->Timestamp_Now =  gmmktime(gmdate('H'), gmdate('i'), gmdate('s'), gmdate('n'), gmdate('j'), gmdate('Y'));
  }
}

$TransTS = new DateClass;
$TransTS->Get_Trans_TS();
echo $TransTS->Timestamp_Now;

What I am stumped with if I combined the function in the class to return array of date timestamps so save having to make loads of object calls eg

class DateClass {

  function Get_Trans_TS() {
    $Trans_TS_Array = array();
    $Trans_TS_Array['Timestamp_Now'] =  gmmktime(gmdate('H'), gmdate('i'), gmdate('s'), gmdate('n'), gmdate('j'), gmdate('Y'));
    $Trans_TS_Array['Timestamp_Tomorrow'] =  gmmktime(gmdate('H'), gmdate('i'), gmdate('s'), gmdate('n') + 1, gmdate('j'), gmdate('Y'));
return $Trans_TS_Array;
  }
}

How would I access the returned variables from the array or am I doing it wrong in the function?.

I have tried this but it fails, in fact I have tried many different ways but it's keeps failing.

$TransTS = new DateClass;
$TransTS->Get_Trans_TS();
echo $TransTS->Timestamp_Now;
echo $TransTS->Timestamp_Tomorrow;

Please shine a light for me to see. Thanks

    Not sure if this what you're looking for but this is one way to approach it:

    
    <?PHP
    class DateClass { 
    
      var $Trans_TS_Array = array();  
    
    
      function Get_Trans_TS() {
        $this->Trans_TS_Array['Timestamp_Now'] =  gmmktime(gmdate('H'), gmdate('i'), gmdate('s'), gmdate('n'), gmdate('j'), gmdate('Y')); 
        $this->Trans_TS_Array['Timestamp_Tomorrow'] =  gmmktime(gmdate('H'), gmdate('i'), gmdate('s'), gmdate('n') + 1, gmdate('j'), gmdate('Y')); 
        return $Trans_TS_Array; 
      } 
    } 
    
    
    $TransTS = new DateClass; 
    $TransTS->Get_Trans_TS(); 
    echo "now: ".$TransTS->Trans_TS_Array[Timestamp_Now]; 
    echo "<br/>";
    echo "tomorrow: ".$TransTS->Trans_TS_Array[Timestamp_Tomorrow]; 
    
    
    ?>
    
    
    
      Pigmaster wrote:

      I have tried this but it fails, in fact I have tried many different ways but it's keeps failing.

      In your first example, you assigned to $this->Timestamp_Now, i.e., a member variable. Later, you called the member function and then accessed the member variable. That is fine.

      In your second example, you assigned to $Trans_TS_Array, i.e., a local variable. Later, you called the member function and then access the member variable, but you assigned to a local variable, not a member variable, hence the problem.

      Since you returned the local variable, you could have written:

      $TransTS = new DateClass;
      $Trans_TS_Array = $TransTS->Get_Trans_TS();
      echo $Trans_TS_Array['Timestamp_Now'];
      echo $Trans_TS_Array['Timestamp_Tomorrow'];

      Actually, one flaw in your DateClass class is that the user has to call Get_Trans_TS. It would be better if the constructor initialised the member variables.

        jeepin81;10984494 wrote:

        Not sure if this what you're looking for but this is one way to approach it:

        echo "now: ".$TransTS->Trans_TS_Array[Timestamp_Now]; 
        echo "tomorrow: ".$TransTS->Trans_TS_Array[Timestamp_Tomorrow]; 

        Thanks jeepin81, the above code did not work for me but in the end it needed ' around Timestamp_Now. eg.

        echo "now: ".$TransTS->Trans_TS_Array['Timestamp_Now']; 

        and then it worked. Must be a config issue on my server, any it works.

        Now looking at Laserlight's suggestion of putting the call into the construct. I did this

        function __construct() {
          $TransTS = new DateClass;
          $TransTS->Get_Trans_TS();
        }
        
        

        But this fails with

        Notice: Undefined variable: TransTS in C:\wamp\www\test\classy.php on line 25
        Notice: Trying to get property of non-object in C:\wamp\www\test\classy.php on line 25

        Line 25 is

        echo "now: ".$TransTS->Trans_TS_Array['Timestamp_Now'];
        
          Write a Reply...