Is it good practice to make environment variables global?
<p>I have some modular ( object and classes ) JavaScript and PHP.  I keep getting advice to get rid of global variables..."they are bad"...I guess because they cause dependencies.</p>

<p>However where should I put environment variables - the best example being path information.</p>

<p>Here is a quintessential example.</p>

<p>The UploadFile class below needs </p>

<pre><code>const     PICTURES = '../pictures/';
</code></pre>

<p>particularly in this spot ( no need to read entire class )</p>

<pre><code>$this-&gt;path_medium = UploadedFile::PICTURES . "$this-&gt;sessionId.jpg";
$this-&gt;path_small = UploadedFile::PICTURES . "$this-&gt;sessionId-1.jpg";
</code></pre>

<p>to no where to upload files to.  I would prefer to put it in a GlobalClass and access it from there.  It makes sense as other code will need to know where pictures go, and I will have only one location to make updates.</p>

<p>Are having environment variables O.K to have as globals so they can be accessed from multiple modules yet edited in only one place.</p>

<pre><code>&lt;?php

/**
 *      Module  :       Model
 *      Name    :       UploadFile
 *      Input   :       File Information
 *      Output  :       Resized Files in .jpg format
 *      Notes   :

 resizeMove() - resizes the picture to $maxMedium and $maxSmall and moves the file to a permanent location.  
 makeDimensions() - calculates the dimensions of the new images so that there is not distortion if possible.
 getImage() - creates a php image for manipulation.
 updateSessionAndDb - updates the mysql table - move out.

 */

    class UploadedFile
    {
    const     PICTURES = '../pictures/';

    private  $originalWidth, 
             $originalHeight, 
             $newWidth, 
             $newHeight, 
             $maxMedium = 50,
             $maxSmall = 20;

    private  $src = NULL;

    private 
             $fileType,
             $fileName,
             $sessionId,
             $path_medium,
             $path_small;

    function __construct($fileType, $fileName)
    {
        $this-&gt;sessionId = Session::get('id');
        $this-&gt;path_medium = UploadedFile::PICTURES . "$this-&gt;sessionId.jpg";
        $this-&gt;path_small = UploadedFile::PICTURES . "$this-&gt;sessionId-1.jpg";
        $this-&gt;fileType = $fileType;
        $this-&gt;fileName = $fileName;
    }

    public function createImages()
    {
        if(move_uploaded_file($this-&gt;fileName, $this-&gt;path_medium))
        {
            if($this-&gt;getImage($this-&gt;path_medium))
            {
                list($this-&gt;originalWidth,$this-&gt;originalHeight)=getimagesize($this-&gt;path_medium);
                $this-&gt;resizeMove($this-&gt;maxMedium,$this-&gt;path_medium);
                $this-&gt;resizeMove($this-&gt;maxSmall,$this-&gt;path_small);
                imagedestroy($this-&gt;src);
            }
        }
    }

    private function resizeMove($max, $path)
    {
        $this-&gt;makeDimensions($max);
        $image_true_color = imagecreatetruecolor($this-&gt;newWidth, $this-&gt;newHeight);
        imagecopyresampled($image_true_color, $this-&gt;src, 0, 0, 0, 0, $this-&gt;newWidth, $this-&gt;newHeight, $this-&gt;
        originalWidth, $this-&gt;originalHeight);
        imagejpeg($image_true_color, $path);
        imagedestroy($image_true_color);
    }

    private function makeDimensions($max)
    {
        $this-&gt;newWidth=$this-&gt;originalWidth; 
        $this-&gt;newHeight=$this-&gt;originalHeight;
        if(($this-&gt;originalWidth &gt; $this-&gt;originalHeight) &amp;&amp; ($this-&gt;originalWidth &gt; $max))
        {
            $this-&gt;newWidth = $max;
            $this-&gt;newHeight = ($max / $this-&gt;originalWidth) * $this-&gt;originalHeight;
        }
        elseif($this-&gt;originalHeight &gt; $this-&gt;originalWidth &amp;&amp; $this-&gt;originalHeight &gt; $max)
        {
            $this-&gt;newHeight = $max;
            $this-&gt;newWidth = ($max / $this-&gt;originalHeight) * $this-&gt;originalWidth;
        } 
        elseif ($this-&gt;originalWidth &gt; $max)
        {
            $this-&gt;newWidth = $this-&gt;newHeight = $max;
        }
    }

    private function getImage($path)
    {
        $type_creators = array( 
            'image/gif' =&gt; 'imagecreatefromgif', 
            'image/pjpeg' =&gt; 'imagecreatefromjpeg', 
            'image/jpeg' =&gt; 'imagecreatefromjpeg', 
            'image/png' =&gt; 'imagecreatefrompng'); 
        if(array_key_exists($this-&gt;fileType, $type_creators)) 
        { 
            $this-&gt;src = $type_creators[$this-&gt;fileType]($path); 
            return true; 
        }
    return false; 
    }
}
</code></pre>

<p><strong>Object Maker Example</strong></p>

<pre><code>class ObjectMaker
{
    public function makeSignUp()
    {
        $DatabaseObject = new Database();
        $TextObject = new Text();
        $MessageObject = new Message();

        $SignUpObject = new ControlSignUp();        
        $SignUpObject-&gt;setObjects($DatabaseObject, $TextObject, $MessageObject);
        return $SignUpObject;
    }
</code></pre>

<p><strong>Object Maker Example Revised</strong></p>

<pre><code>    class ObjectMaker
    {
        public function makeSignUp()
        {
            $DatabaseObject = new Database();
            $TextObject = new Text();
            $MessageObject = new Message();

            return new ControlSignUp( $DatabaseObject, $TextObject, $MessageObject );        
        }
</code></pre>

<p><strong>Notes:</strong></p>

<p>I have may classes similar to SignUp so I can use inheritance to reduce redundancy of object creation by using an ObjectMaker Class - Back to the original problem - I can include injection of Globals not just Objects in this "pattern".</p>
