Jump to content

Passing Variables into auto loaded class files


Go to solution Solved by requinix,

Recommended Posts

It seems that I'm struggling with an issue trying to pass a variable to the class file, but tells me the $db is undefined when I call the User::getUser(). Any ideas how can I achieve passing the $db that is defined in the bootstrap.inc.php and then using it in the auto loaded class file User?

 

 

In my bootstrap.inc.php file which it's included in the index.php file.

<?php
# Autoload the classes
spl_autoload_register("autoload");
# Autoload Composer Libraries
require_once('composer/autoload.php');
# Set up the database connection 
$db = \ParagonIE\EasyDB\Factory::create( 
	'mysql:host='.DB_HOST.';dbname='.DB_NAME, 
	DB_USERNAME, 
	DB_PASSWORD ); 

# Function to load the required classes files 
function autoload($class_name) 
{ 
	# Load classes... 
	if(file_exists(CLASS_PATH.DS.$class_name.'.class.php')) 
	{ 
		require CLASS_PATH.DS.$class_name.'.class.php';
		return; 
	} 
}

And then in my User.class.php file, I called the getUser function, and then it said undefined variable $db.

<?php
class User
{
    public static function getUser()
    {
        $userData = $db->row("SELECT * FROM users WHERE id = ?", 1);
        return $userData;
    }
}

I appreciate any solutions!  :happy-04:

Edited by KyleJR
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.