Jump to content

simple php class basics mistake {Solved}


spoody_goon

Recommended Posts

I am returning to writing in php, I have been refreshing my memory with this tutorial http://www.phpfreaks.com/tutorials/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:
[code]
<?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

}
}

?>[/code]

Here is the simple usage:
[code]
require_once "clsHeaders.php";
$MyHeader = new clsPrintHeaders();
$MyHeader->Title = "Log In";
$ThisHeader = $MyHeader->fntStandardHeader();
print $ThisHeader;
[/code]
Link to comment
Share on other sites

[quote author=spoody_goon link=topic=100062.msg394470#msg394470 date=1152544110]
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
[/quote]

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

[code]
<?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;
?>
[/code]

good luck
Link to comment
Share on other sites

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.
Link to comment
Share on other sites

[quote author=spoody_goon link=topic=100062.msg394647#msg394647 date=1152560263]
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.
[/quote]

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.