Unmanaged PHP Development Tutorials

Death to Global PHP Variables! Long Live Static Objects (References)!

This tutorial teaches you how to create an object with any class name and a variable number of arguments easily.

We're still paving the way for the Singleton pattern :)

Source Code

This is the source code for this tutorial. Enjoy!

<?php
/**
* Global Object storage.
*
* @return object
*/

function MyStorage(){
     
// I'm a .cpp coder, I'm an initialization maniac
     static $Object = null;
     
// If empty, make an object happen
     if(!isset($Object)){
          
$Object = new \stdClass();
          
echo '(instantiated)';
     
}
     
// Return the object
     return $Object;
}
?>

Errata

  1. I say $_GET, $_POST, ... etc are globals. Well, they're super-globals, I know :) But not sure everyone knows the difference.
  2. At minute 16 I say this would work with an array too. Yes, an array of objects exactly as shown in the next function.
    Just wanted to make sure I make sense there. Pure arrays would return copies and defeat the purpose.

It's, oh, so quiet. It's, oh, so still. Sshhh…

I know you have something interesting to say. Don't hold back!

Hello Stranger

Strangers cannot comment as my momma taught me not to talk to strangers.
But you can revoke your stranger status by registering.
Free registration makes you a user with good access, paid membership makes you awesome with full access.