given:

class Foo()
{

function bar()
{
    $var1 = "something";
}


}
$test = new Foo();

$foo_bar = //i need the value var1 here
    class Foo()
    {
    
    function bar()
    {
        $var1 = "something";
        return $var1;   //   <------- you need to return it
    }
    
    }
    $test = new Foo();
    
    $foo_bar = $test->bar();   //   <------- now you can get the value returned by the method
    

      well... thats the thing... i have a dozen variables set within a function... i cant return them all unless i return an array... and that just seems like added overhead to process out the array

        Without knowing the precise functionality, it's hard to say what the best solution(s) might be. Returning an array would make sense if the only purpose of the method is to do something and return all those values to the caller. For my tastes, that would be cleaner than, say, passing a bunch of variables by reference in the function call. The other alternative if those values have some sort of non-transient significance to the object itself would be to make them class variables with public access (or public "get" methods for them). Then the method could set their values, and the client could read any of them in which it was interested.

          heres the specific function:

          	function setDateInfo($day,$month,$year)
          	{
          		//PUT LAST MONTH DATE INTO VARIABLE 
          	    $last_month = getDate(mktime(0, 0, 0, $month - 1, 1, $year)); 
          	    //PUT THIS MONTH DATE INTO VARIABLE 
          	    $this_month = getDate(mktime(0, 0, 0, $month, 1, $year)); 
          	    //PUT NEXT MONTH DATE INTO VARIABLE 
          	    $next_month = getDate(mktime(0, 0, 0, $month + 1, 1, $year)); 
          
              //VARIOUS DATE VARIABLES 
              $first_week_day = $this_month["wday"]; 
              $days_in_this_month = round(($next_month[0] - $this_month[0]) / (60 * 60 * 24)); 
              $days_in_last_month = round(($this_month[0] - $last_month[0]) / (60 * 60 * 24)); 
              $this_month_last_day = getDate(mktime(0, 0, 0, $this_month["mon"], $days_in_this_month, $year)); 
              $days_from_past =  ($days_in_last_month - ($first_week_day - 1)); 
              $days_from_future = 1; 	
          }
          

            i need each of those through the rest of my program

              I'd probably put them into an array and return it. Or to get really OOP about it, I might create a class just for that data structure, instantiate it within the function, set the values a object variables, then return that object.

                an array it is... i can always make it pretty later i suppose 😉

                thanks for the help

                  Write a Reply...