/    Sign up×
Community /Pin to ProfileBookmark

New to Classes, need some advice

I’m redesigning a site using mostely classes, and I’m still trying to learn how to go about doing it. Right now I’m just working on getting a basic HTML structure working. Most of the sites that I’ve seen explaining ways to do it (all 3 of them), say to structure it like this:

[code=php]
class Page {
var $Title;
var $Keywords;
var $Content;

function Display( ) {
echo “<HTML>n<HEAD>n”;
$this->DisplayTitle( );
$this->DisplayKeywords( );
echo “n</HEAD>n<BODY>n”;
echo $this->Content;
echo “n</BODY>n</HTML>n”;
}

function DisplayTitle( ) {
echo “<TITLE>” . $this->Title . “</TITLE>n”;
}

function DisplayKeywords( ) {
echo ‘<META NAME=”keywords” CONTENT=”‘ . $this->Keywords . ‘”>’;
}

function SetContent( $Data ) {
$this->Content = $Data;
}
}[/code]

And I understand that way of doing it. However, I’m wanting to do something else because having the SetContent() function in there, basically just call up the beginning of the html, do the content on the page, and then call up the end of the html. Kinda like this:

[code=php]class HTML {
var $title, $keywords, $description;
var $docType = ‘<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>’;
var $jsBasic = ‘<script type=”text/javascript” src=”https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js”></script>’;

public function BeginHTML() {
$str = $this->docType.’
<html lang=”en” xmlns=”http://www.w3.org/1999/html”>
<head>
<title>’.$this->title.'</title>
<meta name=”robots” content=”INDEX, FOLLOW” />
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<meta name=”keywords” content=”‘.$this->keywords.'” />
<meta name=”description” content=”‘.$this->description.'” />
<link href=”main-style.css” rel=”stylesheet” type=”text/css” />
‘.$this->jsBasic;
$str .= ‘ </head>
<body>
<div class=”fbv_Bodywrap”>’;
}
public function EndHTML() {
$str = ‘
</div>
</body>
</html>’;
}

}
[/code]

When I try to call each of the functions on my page, I’m able to get the Begin, but not the End. Anyone got some suggestions for me?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@DexterMorganMar 26.2011 — Hi, I think you might be looking at some outdated tutorials.

It is considered a bad practice to do output inside a class e.g. outputting html code or whatever.

Plus your BeginHTML/EndHTML methods/functions are not returning anything, so if you really wanted to you could return $str from the methods.

I do not understand why you would need to do this using classes? Could you not just include a beginHTML.php and endHTML.php file?

I use classes for doing stuff, never outputting stuff.

net.tutsplus is a good place for tutorials:

http://net.tutsplus.com/tutorials/php/object-oriented-php-for-beginners/

http://net.tutsplus.com/tutorials/php/oop-in-php/
Copy linkTweet thisAlerts:
@NogDogMar 26.2011 — If I were going to do something like that, I'd try to make it a bit more dynamic, along these lines:
[code=php]<?php
/**
* HTML stuff
*/
class HTML
{
/**
* @var string Page title
*/
public $title;
/**
* @var array Meta keywords
*/
private $keywords = array();
/**
* @var string Meta Description
*/
public $description;
/**
* @var string Doctype
*/
public $docType = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
/**
* @var array JavaScript URL's
*/
private $jsUrls = array(
"https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js"
);
/**
* @var array CSS stylesheet URLs
*/
private $cssUrls = array(
"/css/main-style.css"
);
/**
* Constructor
*/
public function __construct($title)
{
$this->title = ($title);
}
/**
* Add a meta keyword
* @return void
* @param string $keyword
*/
public function addKeyword($keyword)
{
$this->keywords[] = $keyword;
}
/**
* Add a URL for a stylesheet
* @return void
* @param string $url
*/
public function addCssUrl($url)
{
$this->cssUrls[] = $url;
}
/**
* Add a JavaScript URL
*/
public function addJsUrl($url)
{
$this->jsUrls[] = $url;
}
/**
* Get the meta keyword tag
* @return string
*/
private function getKeywords()
{
$keywords = htmlspecialchars(implode(',', $this->keywords));
return "<meta name='keywords' content='$keywords' />n";
}
/**
* Get the JS script tags
* @return string
*/
private function getJs()
{
$text = '';
foreach($this->jsUrls as $url) {
$text .= "<script type='text/javascript' src='$url'></script>n";
}
return $text;
}
/**
* Get the CSS link tags
*/
private function getCss()
{
$text = '';
foreach($this->cssUrls as $url) {
$text .= "<link type='text/css' rel='stylesheet' href='$url' />n";
}
return $text;
}
/**
* Get the start-of-page HTML
* @return string
*/
public function beginHTML()
{
$str = $this->docType . '
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>' . $this->title . '</title>
<meta name="robots" content="INDEX, FOLLOW" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
' . $this->getKeywords() .
'<meta name="description" content="' . $this->description . '" />
' . $this->getCss() . $this->getJs() .
'</head>
<body>
<div class="fbv_Bodywrap">';
return $str;
}
/**
* Get the end of page HTML
* @return $text;
*/
public function endHTML()
{
$str = '
</div>
</body>
</html>';
return $str;
}
}[/code]

Sample Usage:
[code=php]
<?php
require_once 'HTML.class.php';
$html = new HTML("The Title");
$html->description = "This is a test";
$html->addKeyword('foo');
$html->addKeyword('bar');
$html->addCssUrl('/css/another.css');
$html->addJsUrl('/js/test.js');
echo $html->beginHTML();?>
<h1>Test Page</h1>
<p>This is a test.</p>
<?php echo $html->endHTML(); ?>[/code]


Output:
[code=html]<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>The Title</title>
<meta name="robots" content="INDEX, FOLLOW" />
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name='keywords' content='foo,bar' />
<meta name="description" content="This is a test" />
<link type='text/css' rel='stylesheet' href='/css/main-style.css' />
<link type='text/css' rel='stylesheet' href='/css/another.css' />
<script type='text/javascript' src='https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js'></script>
<script type='text/javascript' src='/js/test.js'></script>
</head>
<body>

<div class="fbv_Bodywrap"><h1>Test Page</h1>
<p>This is a test.</p>

</div>
</body>
</html>[/code]
Copy linkTweet thisAlerts:
@beau_kangauthorMar 26.2011 — Thanks for the suggestions. Turns out that I didn't include the return on my EndHTML function. I'm still learning how to build my own classes, so I'm not worried about making it more dynamic just yet. Thank you all for your help and for the tutorials. I'll be bookmarking them for sure.
Copy linkTweet thisAlerts:
@NogDogMar 26.2011 — PS: I meant to mention that the use of the [b]var[/b] keyword for class variables is pretty old-fashioned today. Assuming you are using PHP 5 (and there is no good reason for using an older version, [i]especially[/i] when using object-oriented PHP), you really should be using the access keywords [b]public[/b], [b]protected[/b], and [b]private[/b].

Also, you might find this little blog article useful as a jumping off point: http://www.charles-reace.com/blog/2009/10/27/beginners-corner-learning-object-oriented-php/.
Copy linkTweet thisAlerts:
@beau_kangauthorMar 26.2011 — Yea, I actually changed it later that day after I read a couple of other tutorials. Thank you again for your help.
×

Success!

Help @beau_kang spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.27,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...