Page 1 of 1

Passing Object as default function parameter.

Posted: Wed Feb 06, 2008 9:45 am
by stakes
Hello. Question about passing existing objects as parameters to functions. Example.

Code: Select all

 
 
/* create datbase connection */
 
$db = new db();
 
function getPageHeader($title, $db) {
include('header.php');
}
 
$title = "this is a page";
 
getPageHeader($title);
 
/*then the rest of the page */
 
 
Now there are like 20 pages that calls the getPageHeader function as the title varies from page to page. However the DB object is constant so is there a way to set it as a default value in the getPageHeader function instead of having to do " getPageHeader($title, $db); " on all the pages.

thanks in advance

/Daniel

Re: Passing Object as default function parameter.

Posted: Wed Feb 06, 2008 11:29 am
by Christopher
You should pass the $db object in as a parameter. That is the best way even if it seems like more work.