Jump to content


Photo

Simple OOP question


  • Please log in to reply
2 replies to this topic

#1 pedrobcabral

pedrobcabral
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 15 September 2006 - 05:52 PM

The following does work:
$day = date('d');
echo $day;
The following does not work:
class day {
var $day = date('d');

function display() {
echo $this->day;
}
}

$speak = new day;
$speak->display();

Why the second does not work? The problem it's on var $day = date('d'); but i can't figure out what.
Thank you.

#2 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 15 September 2006 - 05:55 PM

Because the only variables you can define using var is arrays. Others have to be defined in a function. Example:
<?php
class day
{
	var $day;
	
	function day()
	{
		$this->day = date('d');
	}
	
	function display()
	{
		echo $this->day;
	}
}

$speak = new day();
$speak->display();
?>


#3 pedrobcabral

pedrobcabral
  • Members
  • PipPipPip
  • Advanced Member
  • 108 posts

Posted 15 September 2006 - 06:00 PM

thank you very much. :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users