I have a class Page() that I can use to print the (layout) header with.
With the function loadHeader($title) I wa开发者_开发技巧nt to be able to print it on all my pages. The actual code for the header is stored in header.inc.php.
Now before the header I also want certain variables (like one which maintains a database connection) to be passed along with it, so that I can pick it up on all my pages and work with it.
The class looks a bit like this (thined out to prevent a mess):
class Page{
//All vars here
function __construct(){
//constructor code
}
function loadHeader(){
$header = file_get_contents("header.inc.php");
//Some editing of the parsed $header here
//Here I want certain variables to be passed along
return $header;
}
}
The exact code I want to get passed along with the function loadHeader() is:
session_start();
include("db.class.php"); //File which contains the db_class
$db = new db_class;
$db->connect();
Tried doing this with eval() and heredoc within the loadHeader() function but I can't seem to get this working. I'm desperate!
Finally to give you an example on how I want my pages to work:
<?php
include("page.class.php");
$page = new page("Friends");
$page->loadHeader();
?>
Website content with database manipulation
Why don't you just put that code in the Page constructor?
Or make a generic singleton class which will hold your global data, configuration settings, etc?
Or simply add an auto_prepend with that configuration, so that it automatically is included in any page, and you can completely forget about it?
精彩评论