cgm225 Posted December 11, 2007 Share Posted December 11, 2007 I am still very new to OOP, and wanted to try coding a class to generate a basic webpage header so I could simply better understand the syntax and general behavior of a class. I have included my code below, and wanted to know what the community thought of it, my use of "Public" declarations, capitalization, where it could be improved/made more efficient, etc. I appreciate any feedback, as OOP is still somewhat confusing to me. Thank you all in advance! Brendan <?php class framework { Public $title; Public $keywords; Public $description; Public $abstract; Public function Title($title){ $this->Title = $title; } Public function Keywords($keywords){ $this->Keywords = $keywords; } Public function Description($description){ $this->Description = $description; } Public function Abstrt($abstrt){ $this->Abstrt = $abstrt; } Public function Header(){ echo "<head>\n"; echo "\t<title>$this->Title</title>\n\n"; echo "\t<meta name=\"keywords\" content=\"$this->Keywords\" />\n"; echo "\t<meta name=\"description\" content=\"$this->Description\" />\n"; echo "\t<meta name=\"abstract\" content=\"$this->Abstrt\" />\n\n"; echo "</head>\n\n"; } } $webpage = new framework(); $webpage->Title("This is the webpage title!!"); $webpage->Keywords("keyword1, keyword2, keyword3"); $webpage->Description("A brief description of the webpage!"); $webpage->Abstrt("A short webpage abstract!"); $webpage->Header(); ?> Quote Link to comment Share on other sites More sharing options...
tkstock Posted December 11, 2007 Share Posted December 11, 2007 Common practice for Class Names is CapitalCaseLikeThis. Common practice for function names is functionNameLikeThis. Common practice for variale names is $lowercaselikethis. If your class variables are only accessed directly within the class, make them private. You're mixing the case of your variables - variables are case sensitive, so this wouldn't work. In your Abstrt function, you assign $this->Abstrt (which doesn't exist). Just some immediate thoughts... I'm sure others will have more! HTH Quote Link to comment Share on other sites More sharing options...
cgm225 Posted December 12, 2007 Author Share Posted December 12, 2007 Does anyone else have any feedback? Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted December 12, 2007 Share Posted December 12, 2007 In the strictest sense, functions should only return data, they should not output it. Your header() function should actually return a string that you use in your main program logic any way you want. Let's say that after you write your class that forms various parts of a HTML page, you decide you need to send HTML formatted emails. Wow, I have this class that builds HTML pages, but it echos the output, I wish I had made that general purpose and return the content so that I could either echo it or put it into an email, without needed to write another class or edit the one I already have. Keep your code general purpose and reusable. Quote Link to comment Share on other sites More sharing options...
cgm225 Posted December 13, 2007 Author Share Posted December 13, 2007 @PFMaBiSmAd - That advice is so helpful! So, with my header(), how would you return that data? How do you get that returned data formatted as desired (i.e. line breaks, tabs, etc.)? Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 13, 2007 Share Posted December 13, 2007 you create functions just to set data i think that is waste of time and line consuming Quote Link to comment 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.