I'm trying to understand why this isnt working, as I am new to PHP I havent come up with a solution.
Background: I have a DB connection class (Works fine). I want to clean up my code by creating the DB object outside of all of my functions for which I can then use it within each (Instead of creating a new DB object for each function). I pull the data in the DB class, then call the needed method in another file of which I can print/display the data.
Heres an example:
THIS DOES NOT WORK: It produces: Fatal error: Call to a member function getProductsOnSale() on a non-object
require("DB.class.php");
include_once 'functions.php';
$db = new DB();
function displaySalesProducts(){
$salesProduct = $db->getProductsOnSale();
//STUFF METHOD DOES
}
However, this DOES work
require("DB.class.php");
include_once 'functions.php';
function displaySalesProducts(){
$db = new DB();
$salesProduct = $db->getProductsOnSale();
//STUFF METHOD DOES
}
I'm looking for some insight as to why exactly this is happening as I dont have a great grasp of php yet. Thanks!