Page 1 of 1

declaring var in classes

Posted: Mon Jul 26, 2010 9:53 am
by md7dani
This doesn't work:

Code: Select all

class tc_date{
var $today_m = date('n');
var $today_d = date('d');
var $today_y = date('Y');
}
Why can't I declare values like above?

this works better:

Code: Select all

class tc_date{
var $today_m = 07;
var $today_d = 26;
var $today_y = 2010;
}
then I want to use the variables like this:

Code: Select all

(int)$this->today_d  //the value of today 26
thnx!

Re: declaring var in classes

Posted: Mon Jul 26, 2010 10:07 am
by VladSun
You can not put non constant initialization values for your class properties. You should use a constructor and pass these values at object creation (the new operator).

Re: declaring var in classes

Posted: Mon Jul 26, 2010 10:31 am
by Benjamin
:arrow: Moved to PHP - Code

Re: declaring var in classes

Posted: Mon Jul 26, 2010 12:02 pm
by md7dani
thanks for the tip!

I've tried to use a constructor according to an example I found:

Code: Select all

class datum {
var $month;
var $day;
var $year;

function datum($umonth,$uday,$uyear) 
{         
      $this->month = $umonth;
      $this->day = $uday;
      $this->year = $uyear;
   }
}

$datum = new datum(date("n"), date("d"), date("Y"));

$selected = ((int)$datum->day == $i) ? " selected" : "";
The code above doesn't work, I can't see why.

Re: declaring var in classes

Posted: Mon Jul 26, 2010 12:33 pm
by websitesca
I'm not quite sure what you mean by "doesn't work" - do you get a compile error or a runtime error?

I tried your website code and it runs fine. But I did notice that the variable $selected may not contain what you think because $i is not in scope.

I simply did:

Code: Select all

print_r($datum);
And your object works great. I think your last line

Code: Select all

$selected = ((int)$datum->day == $i) ? " selected" : "";
just needs somekind of value for $i. Also you don't need the int cast in your design.

Hope that helps,
Georges
http://www.websites.ca

Re: declaring var in classes

Posted: Mon Jul 26, 2010 2:05 pm
by md7dani
It compiles correctly but I don't get any value on $datum->day, it's empty. Maybe it's because you can't pass a value from a class into another class. Maybe I must put all in the same class, but I don't know if it's possible to create a new operator in a class.

$i has the value of 1 to 31 for the days in a month, it's in a loop. So you compare a value with $i, when they are equal, the value is selected. So when it is today's date, 26, it will loop until 26 == $i. I want ($datum->day == $i) but $datum->day is empty so it prints/selects 'day' instead.

I tried print_r($datum) and it prints out :

datum Object ( [month] => 7 [day] => 26 [year] => 2010 )

Here is the problem:

Code: Select all

class datum {
var $month;
var $day;
var $year;

   function datum($umonth,$uday,$uyear) 
   {         //$today_m = date("n"), $today_d = date("d"), $today_y = date("Y")
       $this->month = $umonth;
	   $this->day = $uday;
	   $this->year = $uyear;
   }
}
$datum = new datum(date("n"), 26,date("Y"));
print_r($datum);   //this works fine 

class tc_calendar
{
 	//write the select box of days
	function writeDay(){
		echo("<select name=\"".$this->objname."_day\" id=\"".$this->objname."_day\" onChange=\"javascript:tc_setDay('".$this->objname."', this[this.selectedIndex].value, '$this->path');\" class=\"tcday\">");
		echo("<option value=\"00\">Day</option>");
		for($i=1; $i<=31; $i++)
		{
		 $selected = ((int)$this->day == $i) ? " selected" : "";			
                                 echo("<option value=\"".str_pad($i, 2 , "0", STR_PAD_LEFT)."\"$selected>$i</option>");
                                }
	      echo("</select> ");
	      echo $datum . " " . $i;   
	} //end function
} //end class
$datum is empty and also $datum->day. Can I pass the value of the new operator into the class tc_calendar?

Re: declaring var in classes

Posted: Mon Jul 26, 2010 2:53 pm
by Benjamin
:arrow: Use code tags when posting code in the forums.

Re: declaring var in classes

Posted: Mon Jul 26, 2010 5:29 pm
by md7dani
solved with a return function() :

Code: Select all

function datum_d() 
   {         
    $a = date("d");
    return $a;
   }

$this->datum_d()