Jump to content

global variables


php_guest

Recommended Posts

I use $db object to handle database function like $db->get_row for example. Is there any way that I don't need to put into every function global $db or do I actually need to repeat in every function global $db?

 

function example(){
   global $db //Do I need to repeat this in each function?
}

Link to comment
Share on other sites

Hi , I would recommend using a framework i.e. a mvc framework like Zend which uses object orientation.

class DB_Object {
protected $_db;

/* Getters and Setters*/

public function setDb($value) {

	$this->_db = $value;
}

}

 

Then you can reference the member variable.

Link to comment
Share on other sites

in your case yes,

 

but without a framework, I would at least have a static class where I can reference my db object like so

class Database 
{ 
    // Store the single instance of Database 
    private static $m_pInstance; 

    private function __construct() { ... } 

    public static function getInstance() 
    { 
        if (!self::$m_pInstance) 
        { 
            self::$m_pInstance = new Database(); 
        } 

        return self::$m_pInstance; 
    } 
}  

 

Then use that static object as such

$pDatabase = Database::getInstance(); 
$aResult = $pDatabase->query('...');  

 

globals are not nice to work with.

 

Witnot

Link to comment
Share on other sites

You pass the $db object into the function as a parameter when you call the function, which is what the $value parameter is in the code witnot1 posted. It does not matter if you are using a function or a class method.

 

The reason you pass it in as a parameter, instead of using the global keyword, is so that your function or class remains general purpose and is not dependent on the specific name of a variable in the calling code.

 

Suppose that you need to have two different database connections at the same time in an application. If you use the global keyword, you would need to use more code to shuffle the two different connections back and forth between the $db variable before each call of your function or you would need to duplicate your function and modify the code in one copy to use the different db object name.

 

However, why would you go through the extra work and coding to do that? Functions and classes are supposed to make writing code easier, not create more work. By passing the db object (or any other main program variable) into the function when you call it, you can use any number of different db objects in your code simply by putting the correct one in as a parameter when you call your function. You can write and test your function once and then use it with one db object or 10 different db objects with the minimum lines of code or extra work keeping track of what you are doing at the current time in your program.

Link to comment
Share on other sites

Using a static class has the same disadvantages as using the global keyword, your code is dependent on the specific name you have hard coded into the function/class that is using it.

 

Extrapolate this problem to a different example of a user object ($user instead of a $db object), where you might have an administrator logged into a site and you want to create a page that lets him access and edit a specific user account information. By keeping your functions and classes general purpose, doing this would be simple. The page would check if the current visitor (the admin) is logged in by putting the $user variable into the function calls and then simply create a second instance of the user class (in $edit_user for example) for the visitor he wants to access. You would simply reuse and call your existing functions/classes with the correct instance of the $user or $edit_user that you need at the time.

Link to comment
Share on other sites

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.