Jump to content

Using variabled defined on the page in the page's constructer method?


Recommended Posts

I know it's been a while since I've done anything in PHP but I didn't think I was this rusty. Very basic page class, I love using construct and destruct for this, keeps my other files clean. :) As you can see in my first example, the title variable for the page class is defined and echoed in the title element for the page. The title variable is defined on the page itself as each page has it's own title. If they were all the same, that would defeat the purpose not to mention play havoc on search engine results but that's for another thread. I know the issue is that construct is being called as soon as I call the page class and doesn't wait for me to define the title variable. How do I make this work but keep the code clean and lean.

 

<?php
class page {
var $title;
function __construct(){
	echo
	"<html>\n<head>\n".
	'<title>'.$this->title.'</title>'."\n".
	"</head>\n<body>\n".
}
function __destruct(){
	echo
	"\n</body>\n</html>";
}
}?>

 

<?php
require_once('lib/master.inc.php');
$page = new page;
$page->title = "Main Page";
?>

unless i'm missing something, why not:

 

<?php
class page {
var $title;
function __construct($title){
	echo
	"<html>\n<head>\n".
	'<title>'.$title.'</title>'."\n".
	"</head>\n<body>\n".
}
function __destruct(){
	echo
	"\n</body>\n</html>";
}
}?>

 

<?php
require_once('lib/master.inc.php');
$page = new page('Main Page');
?>

Yea... I figured that out. Is it needed that I declare $title as a variable of the page class like in your example? I didn't and it works:

 

<?php
class page {	
function __construct($title){
	echo
	"<html>\n<head>\n".
	'<title>'.$title.'</title>'."\n".
	'<link rel="stylesheet" type="text/css" href="skins/default/style.css">'."\n".
	"</head>\n<body>\n";
}
function __destruct(){
	echo
	"\n</body>\n</html>";
}
}?>

 

Plus, this method uses even less code than the first example I posted, though it a small difference.

Is it needed that I declare $title as a variable of the page class like in your example?

 

not at all. it was just my cut and paste job not cleaning up afterwards :D the var is declared via the function parameter, so no need whatsoever. whether this is good practice, i do not know - i'd guess not, as your constructor closes up the head and only allows a change in page title. so if you need to include JS files, additional stylesheets or others other than the skins/default/style.css, you'd run in to problems. using your current way, you would be better passing an array into the constructor for later use, so rather than:

 

function __construct($title)

and:

'<title>'.$title.'</title>'."\n".

use:

function __construct($params = array())

and

'<title>'.$params['title'].'</title>'."\n".

 

which would allow for additional changes without changing code that already uses the class.

 

Having said all that though, it might be worth looking at template engines of some form. It'll take away all of your problems but give you tonnes more flexibility - and ultimately achieve a "good practice" goal of seperating HTML from your main PHP logic. Have a read here: http://www.massassi.com/php/articles/template_engines/

 

Cheers

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.