Passing Object as default function parameter.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
stakes
Forum Commoner
Posts: 48
Joined: Tue Jun 12, 2007 12:05 pm

Passing Object as default function parameter.

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Passing Object as default function parameter.

Post by Christopher »

You should pass the $db object in as a parameter. That is the best way even if it seems like more work.
(#10850)
Post Reply