Jump to content

Generating Webpage Header with OOP..


cgm225

Recommended Posts

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();

?>

Link to comment
Share on other sites

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

 

 

Link to comment
Share on other sites

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.

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.