maexus Posted April 24, 2007 Share Posted April 24, 2007 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"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/48506-using-variabled-defined-on-the-page-in-the-pages-constructer-method/ Share on other sites More sharing options...
redbullmarky Posted April 24, 2007 Share Posted April 24, 2007 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'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/48506-using-variabled-defined-on-the-page-in-the-pages-constructer-method/#findComment-237294 Share on other sites More sharing options...
maexus Posted April 24, 2007 Author Share Posted April 24, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/48506-using-variabled-defined-on-the-page-in-the-pages-constructer-method/#findComment-237491 Share on other sites More sharing options...
redbullmarky Posted April 24, 2007 Share Posted April 24, 2007 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 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 Quote Link to comment https://forums.phpfreaks.com/topic/48506-using-variabled-defined-on-the-page-in-the-pages-constructer-method/#findComment-237513 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.