spoody_goon Posted July 10, 2006 Share Posted July 10, 2006 I am returning to writing in php, I have been refreshing my memory with this tutorial http://www.phpfreaks.com/tutorials/48/1.phpA 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 MuchHere is the simple class:[code]<?phpclass 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] Quote Link to comment https://forums.phpfreaks.com/topic/14184-simple-php-class-basics-mistake-solved/ Share on other sites More sharing options...
designationlocutus Posted July 10, 2006 Share Posted July 10, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14184-simple-php-class-basics-mistake-solved/#findComment-55593 Share on other sites More sharing options...
obsidian Posted July 10, 2006 Share Posted July 10, 2006 [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]<?phpclass 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 Quote Link to comment https://forums.phpfreaks.com/topic/14184-simple-php-class-basics-mistake-solved/#findComment-55604 Share on other sites More sharing options...
spoody_goon Posted July 10, 2006 Author Share Posted July 10, 2006 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 {} bracketsThanks much. Quote Link to comment https://forums.phpfreaks.com/topic/14184-simple-php-class-basics-mistake-solved/#findComment-55754 Share on other sites More sharing options...
obsidian Posted July 11, 2006 Share Posted July 11, 2006 [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 {} bracketsThanks 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. Quote Link to comment https://forums.phpfreaks.com/topic/14184-simple-php-class-basics-mistake-solved/#findComment-56249 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.