Breadcrumbs

Global Variables

Need IT certification? CBT Planet (external link) offers A+ certification and management training to help you better understand computers and ultimately to improve your business today!

Globals have been a long standing factor in numerous programming languages. Globals are variables within the global scope or variables that do not belong to a set subroutine, which makes them available in any context of an application. Upon the popularity of object-oriented programming (external link), the use of globals was highly discouraged. The purpose of globals does not properly interface with object-oriented applications, as it does with procedural programming, which is debatable in itself.

Problems with Globals

The main risk of using globals is creating complex dependencies. When multiple parts of an application began to depend on a global variable - remembering that a global variable can be accessed and changed anywhere - often code will clash. The dependency is considered complex because it is fluid and could present problems if one part of the application tinkers with it too much.

Another problem created with global variables is creating mutual dependencies. If each of your classes relies on global variables, you would have to rewrite the entire class or modify the code in which the class is being implemented just to use it in another application. One of the nice benefits of proper OOP is that you can easily reuse components from another application, but they have to be self-reliant first. Therefore, proper OOP will not interface well with globals.

To demonstrate complex dependencies, let us create a scenario where we have two classes - Cat and Dog - each with one instance. They each have one method that prints out the sound of that animal - meow or bark. Both classes rely on the global variable $times to determine how many times to meow or bark.

Code: PHP

<?php
class Cat {
    function 
meow() {
        global 
$times;
        
$final NULL;
        
        for (
$x 0$x $times$x++) {
            
$final .= 'Meow! ';
        }
        
        return 
$final;
    }
}
?>

Code: PHP

<?php
class Dog {
    function 
bark() {
        global 
$times;
        
$final NULL;
        
        for (
$x 0$x $times$x++) {
            
$final .= 'Bark! ';
        }
        
        return 
$final;
    }
}
?>

If neither class modifies the $times variable then everything runs smoothly.

Code: PHP

<?php
$times 
5;

$cat = new Cat;
$dog = new Dog;

echo 
$cat->meow().$dog->bark();
?>

Meow! Meow! Meow! Meow! Meow!
Bark! Bark! Bark! Bark! Bark!

Now, what happens if the Cat class modifies the $times variable? All of a sudden our Dog class does not bark the intended amount of times.

Code: PHP

<?php
class Cat {
    function 
meow() {
        global 
$times;
        
$final NULL;
        
        for (
$x 0$x $times$x++) {
            
$final .= 'Meow! ';
        }
        
        
$times 1;
        return 
$final;
    }
}
?>

Meow! Meow! Meow! Meow! Meow!
Bark!

Even though this is not the best example - the problem is hard to demonstrate in little code - you can see how global variables can create clashing code very easily. The problem is more prominent with larger volumes of code, in which one simple mistake with global variables could throw off the entire application.

While the problem can be avoided, it is best to rid yourself of it all together and eliminate the problem from ever even occurring.

The Singleton Gone Wrong!

Since global variables became a discouraged practice, the concept of globals was jettisoned out the window. Programmers, wanting an easy replacement, began using a popular design pattern - the Singleton (external link).

The purpose of the Singleton design pattern is to provide one instance of a class to an entire application, usually stored as a static variable managed by a function. Essentially, a Singleton object is a global (technically it is a global instance); therefore, it faces relatively the same problems as globals and should be used with extreme caution.

Pages:

  • 1
  • 2