declaring var in classes

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
md7dani
Forum Newbie
Posts: 13
Joined: Fri Oct 23, 2009 1:55 am

declaring var in classes

Post 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!
Last edited by Benjamin on Mon Jul 26, 2010 10:30 am, edited 2 times in total.
Reason: Added [syntax=php] tags.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: declaring var in classes

Post 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).
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: declaring var in classes

Post by Benjamin »

:arrow: Moved to PHP - Code
md7dani
Forum Newbie
Posts: 13
Joined: Fri Oct 23, 2009 1:55 am

Re: declaring var in classes

Post 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.
Last edited by Benjamin on Mon Jul 26, 2010 12:26 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
websitesca
Forum Newbie
Posts: 16
Joined: Fri Jul 09, 2010 2:47 pm

Re: declaring var in classes

Post 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
md7dani
Forum Newbie
Posts: 13
Joined: Fri Oct 23, 2009 1:55 am

Re: declaring var in classes

Post 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?
Last edited by Benjamin on Mon Jul 26, 2010 2:52 pm, edited 1 time in total.
Reason: Added [syntax=php] tags.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: declaring var in classes

Post by Benjamin »

:arrow: Use code tags when posting code in the forums.
md7dani
Forum Newbie
Posts: 13
Joined: Fri Oct 23, 2009 1:55 am

Re: declaring var in classes

Post by md7dani »

solved with a return function() :

Code: Select all

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

$this->datum_d()
Post Reply