Jump to content

Archived

This topic is now archived and is closed to further replies.

spoody_goon

simple php class basics mistake {Solved}

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]

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.