Jump to content


Photo

simple php class basics mistake {Solved}


  • Please log in to reply
4 replies to this topic

#1 spoody_goon

spoody_goon
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 10 July 2006 - 03:08 PM

I am returning to writing in php, I have been refreshing my memory with this tutorial http://www.phpfreaks...orials/48/1.php
A great tutorial but I must be having a brain fart. The problem is that the vaiables I set via $MyHeader->Title = "Log In"; don't get to the functions, they are empty strings at the functions. I suspect I am not using the pointer $this-> correctly but I just am missing the point.
Thanks Much

Here is the simple class:
<?php

class clsPrintHeaders
{

	var $Location;
	var $Title;
	
	function fntRedirectHeader()
	{
		header("Location: $Location");
	}
	
	function fntStandardHeader()
	{
		
		$MyString = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
		$MyString .= "<html>\n";
		$MyString .= "<head>\n";
		$MyString .= "<Title>GoonMail $Title</Title>\n";
		$MyString .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
		$MyString .= "<meta http-equiv=\"Pragma\" content=\"no-cache\" />\n";
		$MyString .= "<link href=\"default.css\" rel=\"stylesheet\" type=\"text/css\">\n";
		$MyString .= "</head>\n";
		$MyString .= "<body>\n";
		return $MyString;

	}
	
	function fntStandardExit()
	{
		?>
		
		</body>
		</html>
		
		<?php

	}
}

?>

Here is the simple usage:
require_once "clsHeaders.php";
$MyHeader = new clsPrintHeaders();
$MyHeader->Title = "Log In";
$ThisHeader = $MyHeader->fntStandardHeader();
print $ThisHeader;


#2 designationlocutus

designationlocutus
  • Members
  • PipPipPip
  • Advanced Member
  • 62 posts

Posted 10 July 2006 - 03:13 PM

Been a while since I've used classes, but I think you might need a constructor function in there so your class can be instantiated properly.

#3 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 10 July 2006 - 03:39 PM

The problem is that the vaiables I set via $MyHeader->Title = "Log In"; don't get to the functions, they are empty strings at the functions. I suspect I am not using the pointer $this-> correctly


that's exactly it. when referencing a member variable from within a member function, you need to use $this->:

<?php
class clsPrintHeaders
{

	var $Location;
	var $Title;
	
	function fntRedirectHeader()
	{
		header("Location: {$this->Location}");
	}


	function fntStandardHeader()
	{
		$MyString = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">\n";
		$MyString .= "<html>\n";
		$MyString .= "<head>\n";
		$MyString .= "<Title>GoonMail {$this->Title}</Title>\n";
		$MyString .= "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=iso-8859-1\">\n";
		$MyString .= "<meta http-equiv=\"Pragma\" content=\"no-cache\" />\n";
		$MyString .= "<link href=\"default.css\" rel=\"stylesheet\" type=\"text/css\">\n";
		$MyString .= "</head>\n";
		$MyString .= "<body>\n";
		return $MyString;

	}
	
}

$MyHeader = new clsPrintHeaders();
$MyHeader->Title = "Log In";
$ThisHeader = $MyHeader->fntStandardHeader();
print $ThisHeader;
?>

good luck
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx

#4 spoody_goon

spoody_goon
  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 10 July 2006 - 07:37 PM

Oh geez I see the pointer $this-> is the only part of the variable that requires the $ symbol.

I have had another reply from another forum that does not incase the pointer/variable in {}. Is there specific uses that require the use of {} brackets

Thanks much.

#5 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 11 July 2006 - 03:02 PM

Oh geez I see the pointer $this-> is the only part of the variable that requires the $ symbol.

I have had another reply from another forum that does not incase the pointer/variable in {}. Is there specific uses that require the use of {} brackets

Thanks much.


as far as requiring the brackets, PHP will interpret the parts of the statement in the brackets first, so there are times that you can use them to override precidence issues, but they are not necessarily required.
You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users