PHP: How not to pollute the global scope

Posted on March 28, 2009

An emerging trend in JavaScript is to wrap entire programs in a single object to prevent conflicts with other scripts. The same can be done with PHP, something I haven’t seen done very often.

If you ever want to combine frameworks you may discover that they use similar names for some variables and functions. This can lead to unexpected output and security holes. If you’re planning on writing a framework of your own, consider wrapping it in an object.

In PHP5 it’s possible to pass objects by reference which means you can send an instance of a class to another class. This way you can nest classes and access the parent.

core.php:

class core
{
    public
        $var = 'This is a global variable.';
 
    const
        CONST_VAR = 'This is a constant.';
 
    // This function is executed when core is initialized.
    function __construct()
    {
        include('classes/foo.php');
 
        $this->foo = new foo($this);
    }
 
    // All the main functions go here.
}

foo.php:

class foo
{
    private $core;
 
    function __construct($core)
    {
        // Reference to the core object.
        $this->core = $core;
    }
 
    function hello_world()
    {
        echo $this->core->var;
    }
}

In this example “core” is the site’s main object containing global variables and functions. When we create an instance of class “foo”, we send a reference to the core object (“$this”) as a parameter. On a regular page the classes, variables and function can be accessed like this:

page.php

include('classes/core.php');
 
$core = new core();
 
echo $core->var; // output: This is a global variable.
echo core::CONST_VAR; // output: This is a constant.
 
$core->foo->hello_world(); // output: This is a global variable.

This way there is only one variable in the global scope: $core. Another advantage is that all variables declared in core are global and accessible throughout the whole program.

Scroll to top

Comments (7)

  • Thanks for pointing me to this, even before this article. It really helped me working out my framework more efficiently and still keep the overview in my classes.

    Posted by CoenJacobs on March 30, 2009 Reply

  • Do you need to use the “&” operator to pass a reference? Objects are merely references anyway, right?

    Posted by Tim Cinel on April 28, 2009 Reply

  • You're right, it's not necessary to send objects by reference. I updated the post, thanks!

    Posted by ElbertF on May 02, 2009 Reply

  • You're right, it's not necessary to pass objects by reference. I updated the post, thanks!

    Posted by ElbertF on May 02, 2009 Reply

  • You're right, it's not necessary to pass objects by reference. I updated the post, thanks!

    Posted by ElbertF on May 02, 2009 Reply

  • There is a typo cont should be const, also after extensive testing (like 3 min) I found that hello_world(); isn't public at all… only thing I found is making $this->foo = new foo($this);

    but that makes it recursive … *help*

    Posted by svennson56 on February 28, 2010 Reply

  • Changed the typo, thanks. I also changed $foo to $this->foo, why would recursion be a problem?

    Posted by ElbertF on March 01, 2010 Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="" highlight="">

Fork me on GitHub